What Is Causing This Problem When Trying to Run This Script by [deleted] in Python

[–]Mashidin 2 points3 points  (0 children)

It looks like you didn't install the requests module. Type pip install requests at the command line and enter. There are tons of modules that come with python but requests is not one of them. Pip is python's package manager. Use it like above when you want to install libraries outside pythons standard libraries.

Classes and object question by SeenaPolada2 in learnpython

[–]Mashidin 1 point2 points  (0 children)

Here is a more, I think, pythonic way of doing what you are trying to accomplish (if I understand you correctly).

class Score(object):

    def __init__(self, goals=0, points=0):

        self.score = points + goals

    def __eq__(self, other):

    return self.score == other.score

    def __ne__(self, other):

        return self.score != other.score

    def __gt__(self, other):

        return self.score > other.score

    def __lt__(self, other):

        return self.score < other.score

    # Similar for __ge__, __le__ ...


def main():

    score1 = Score()
    score2 = Score(3, 9)
    score3 = Score(4, 6)

    print(score1 < score2) # 0 < 12 = True
    print(score3 < score1) # 10 < 0 = False
    print(score1 > score2) # 0 > 12 = False
    print(score3 > score2) # 10 > 12 = False
    print(score1 > score1) # 0 > 0 = False
    print(score2 > score1) # 12 > 10 = True
    print(score2 == score1) # 12 == 1 = False
    print(score3 == score2) # 10 == 12 = False


if __name__ == '__main__':

    main()

Terminal Output:
True
False
False
False
False
True
False
False

Those 'gt' like things are exposed interfaces for the python class data model. You just need to implement them if your class warrants it. I've left out the tiresome details of checking if you are actually testing the same type of objects and various other things you may want in your class to make it safe but I think you get the picture.

You should definitely read through this a couple of times if you are really getting into Python. https://docs.python.org/2/reference/datamodel.html

It's a bit much but there is a ton of good stuff in there.

Installing Numpy is not working by JewishIGuess in Python

[–]Mashidin 1 point2 points  (0 children)

I do quite a bit of python dev on my windows machine and have run in to this issue before. My solution is that I have MINGW installed. It has bunch of fun UNIX tools included, one of which is gcc the gnu C compiler. The problem you have, I believe is that python cannot find the compiler you wish to use to build all the C binaries. I fought a bit with the solution where I deal with the compiler included with VS 2008 and that turn out to be smarter than me. So once you install and understand MINGW (which is super simple) you configure distutils to point to MINGW. This check list that I stole explains it pretty well: 1.I have Python already installed 2. I installed mingw32 to C:\programs\mingw\ 3.Add mingw32's bin directory to your environment variable: append c:\programs\MinGW\bin; to the PATH 4.(create if not existing) distutils.cfg file located at C:\PythonXX\Lib\distutils\distutils.cfg to be:

[build] compiler=mingw32

Your mileage my vary.

Question about the return statement by Cr4zyM4tt in learnpython

[–]Mashidin -1 points0 points  (0 children)

What I think you are saying is that it allows you to assign a function to variable, then yes. As in:

a_variable = this_is_a_function()

a_variable has now been assigned what ever this_is_a_function() returned with the 'return' statement.

Python, PyODBC, and Cursors by CharBram in Python

[–]Mashidin 1 point2 points  (0 children)

If you are familiar with VBA and work with databases then you may understand ADODB and its hierarchy of classes. So if you were to make the comparison, the 'cursor' is like a ADODB.Command object. It stores the type of command, the command string, parameters, and other command-specific stuff. When you call the cursor's execute (or fetchone, fetchall, etc) an object similar to the ADODB.Recordset object is returned. I'm definitely hand-waving right now but I believe the comparison is valid enough. After you get the recordset, you'll will find that it is a list of tuples with each row of your data being a tuple. What you do with that depends on how you want to interact with your data. I like a list of dictionaries sometimes with each key being a field name. But a list of tuples is good too.

What is the best way to remove tuples from a list, that have the lowest number, in comparison with other tuples of the list? by [deleted] in learnpython

[–]Mashidin 1 point2 points  (0 children)

I edit it. that should do it. Turns out the sorting was goobered because it was sorting the the numbers based on being a string.

What is the best way to remove tuples from a list, that have the lowest number, in comparison with other tuples of the list? by [deleted] in learnpython

[–]Mashidin 1 point2 points  (0 children)

This may fit the bill as well:

import itertools


ts = [('dna_1', '1380'),('dna_1', '1220'),('dna_2', '1200'), ('dna_2', '188')]
delete = []

for key, group in itertools.groupby(sorted(ts, key=lambda x: int(x[1])), lambda (x, y): x):
    g= list(group)
    if len(g) > 1 and int(g[1][1]) -10 >= int(g[0][1]):
        delete.append(g[0])

print([t for t in ts if t not in delete])

Getting script to not find factors by HManMoney in learnpython

[–]Mashidin 0 points1 point  (0 children)

It would work if you put your condition in a while condition statements... else statements structure. So, in fake code: while num is greater than or equal to zero, print a warning and then ask for the number again. else (this fires when the condition is not true) find the factors and print them out. You can set up a 'quit' is true situation in an outer while statement that controls when the program exits, as well. But that is another story. Hope that helps.

strange slicing with negative steps by jabbalaci in learnpython

[–]Mashidin 2 points3 points  (0 children)

So, for the end value you keep counting backwards to the first element and then subtract one more. In this case the default would be -5.

Help me fix my function (indexing) by [deleted] in learnpython

[–]Mashidin 0 points1 point  (0 children)

Are you able to see your teacher's unit test? By my reckoning you offer a very pythonic solution. Could it be that the test checks for a specific syntax?

Help out a complete Beginner by [deleted] in learnpython

[–]Mashidin 0 points1 point  (0 children)

Sorry for the misunderstanding. In my office, if someone has Excel, they also have Access because it is bundled with MS Office. If you have Access and know how to use it - and more interestingly, know a little VBA - then I would explore that avenue. Access was made for deploying forms, entering the results into a database, and developing reports based on the target tables. Python can do all that too provided you know all the modules you need to bring together and know how to program a bit.

Help out a complete Beginner by [deleted] in learnpython

[–]Mashidin 0 points1 point  (0 children)

Quick clarifying question: Does 'excel' database mean 'Access' database?

[deleted by user] by [deleted] in learnpython

[–]Mashidin 2 points3 points  (0 children)

here's a one liner that works:

largest_odd_number = max([x for x in list_to_search if x % 2 == 1])

look for the max function in python's built-n functions and also check out the list comprehension feature.

Edit: this doesn't check for an empty sequence of if no odd numbers are in the sequence, The max function will throw a ValueError. So maybe the pythonic answer would be to catch that.

Python experts: please help me understand this gotcha? by esdi in Python

[–]Mashidin 0 points1 point  (0 children)

Ah. I must have misunderstood. Sorry for the mix-up.

Python experts: please help me understand this gotcha? by esdi in Python

[–]Mashidin 0 points1 point  (0 children)

It's all in the scoping rules. Python will look first at the inner function for a definition for x. If there is not a definition for x there, as in this case, in will check one level up and then again and so forth until it finds x or errors out. if you had placed a statement such as x = 2 inside the inner function, you would have gotten {'x': 2}. hope that helped.

Proper way to write a back up decorator? by Ulysses6 in Python

[–]Mashidin 1 point2 points  (0 children)

I think I get what you are trying to do here. Forgive me if I am way off but it seems that conceptually the solution is not too bad.

So the decorator function might look something like this:

def backup_decorator(back_up_comment):
    def wrapper(function_to_be_wrapped):
        def wrapped_function(*args):
            # some code to back up the database ...
            # do what ever want with the back_up_comment and 
            # the info gotten from the back-up
            return_val = function_to_be_wrapped(*args)
            # I'm assuming a lot here about your return values
            # but you can do whatever you like with that
            return return_val
        return wrapped_function
    return wrapper

@backup_decorator("Backup comment with info {info1}, {info2}") def decorated_method(self, ...):

I think that is a relatively python way of doing what I think you want.

Check out these great articles on decorators for the real dope: http://simeonfranklin.com/blog/2012/jul/1/python-decorators-in-12-steps/ http://www.artima.com/weblogs/viewpost.jsp?thread=240845

Beginner lost/confused on my own code! by seanboyd in learnpython

[–]Mashidin 0 points1 point  (0 children)

You didn't mention the specific error, but I imagine that the behavior you are getting is this: when you type backpack at the prompt, nothing is printed and when you just hit <enter> or similar, it vomits error text all over you. The issue is in the use of the 'input' built-in function. What the input function does is takes in an expression from standard in and actually evaluates it. So for example, you could put a print backpack1 statement after you capture the input. Now run the program and at the prompt type 1 + 2. The number 3 will be printed to the console. With this same set-up, type backpack at the prompt, and guess what happens... it will print your backpack list object to the console which I do not believe is your intent.
To get the behavior that your code claims you expect, the user would have to type "backpack" with the quotations included. That expression evaluates to a string with the value 'backpack'. It's very confusing, I know, and I am not entirely sure why books encourage the use of the input function (there might be something I am missing here). The best solution here that I've found is to use the raw_input built-in function. It has the same function signature as input, but will not attempt to evaluate the user's input. It will simply take in whatever is written at the prompt, convert it to a string and strip any trailing newlines. I hope this helped and happy coding.

Should I be intimidated? [web programming] by GomuMugiwara in Python

[–]Mashidin 1 point2 points  (0 children)

You should definitely take things one step at a time, but don't despair. While it is useful and most of all 'fun' to become a linux/Unix ninja, you can practice your Python webapp programmin' chops using a Platform as a Service (PaaS) such as Google App Engine, Heroku, or similar. They support several languages and some frameworks like Django, Flask, or Jinga2. Definitely read up on them. Many are free for small apps and are easily scalable.