Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
evilentity
Jun 25, 2010

The King of Swag posted:

Serialization: I realize that Python has the pickling functionality, but it's inherently unsafe and apparently does way more than I actually want it to (just save the values of the instance variables; I don't care about restoring the object itself). In the end I've resorted to writing my own serialization code utilizing the struct class--unfortunately the lack of fine-grained data control and the beauty that are void pointers actually makes this much more complicated than the equivalent C code.

This should do the trick.

Python code:
def save_instance_vars(object, path):
    with open(path, 'w') as f:
    	variables = vars(object)
   	for (variable, value) in variables.items():
            line = 'variable:{variable}, value:{value}'.format(variable=variable, value=value)
	        f.write(line)
Src

Adbot
ADBOT LOVES YOU

evilentity
Jun 25, 2010

The King of Swag posted:

I've seen that before and the problem is that I need to save my data out as binary and be able to easily read it all back. Using that trick, saving would be easy but loading would require a lot of parsing. My current method of an explicit file format with run lengths embedded for blocks of data is a lot more succinct with no need for parsing of data beyond reading the length of a block, loading that block into memory and reading the next. Although I'm all ears if there is an easy Pythonesque way to load your example.

Parsing bad? Json to the rescue!

Python code:
import json

class Foo(object):
    def __init__(self):
        self.a = 1
        self.b = 2

def save_vars(object, path):
    with open(path, 'w') as f:
        json.dump(vars(object), f)
        
def load_vars(path):
    with open(path, 'r') as f:
        return json.load(f)            
            
if __name__ == '__main__':
    foo = Foo()
    print vars(foo)
    save_vars(foo, r'c:\temp.json')
    print load_vars(r'c:\temp.json')

evilentity
Jun 25, 2010
Or you could send the exe :v:

http://www.pyinstaller.org/

Works for me.

evilentity
Jun 25, 2010

JetsGuy posted:

Quick (stupid) question. I want to make a bunch of lists before going into a loop (that will append data into these lists).
You dont need outer [] there, I think. Is there some specific reason for them? A little cleaner like that.
Python code:
oct_x, nov_x, dec_x, jan_x = [], [], [], []
oct_xerr, nov_xerr, dec_xerr, jan_xerr = [], [], [], []
oct_y, nov_y, dec_y, jan_y = [], [], [], []
oct_yerr, nov_yerr, dec_yerr, jan_yerr = [], [], [], []
You have a bunch of them, maybe some other structure would be better? Hard to tell without more code. I guess you could do something silly like this.
Python code:
monthData = {}
months = ['oct_x', 'nov_x', 'dec_x', 'jan_x'...]
for month in months:
    monthData[month] = []
You could even built in dict but that pretty unclean. But it wont really help much. Dont hit me.

Some better dict structure would probably be better, as posted above.

evilentity fucked around with this message at 00:32 on Dec 6, 2012

evilentity
Jun 25, 2010

Drunk Badger posted:

Here's an example from a routing simulator I'm building.

What I want it to do is give me a new set of numbers on each cycle of the while loop. Running that just produces the same random numbers while the program is run.


It seems to be running the function for each element in 'noderoute = [...', once it gets to that line of code, as I can put a print function and it will display 6 lines of text before it starts putting out the numbers. I know I can just do a bunch of if loops, but I'm wondering if there's a shorter way to do what I want.

As was said above, you get same values each time because they are evaluated when you add them to the list not in the loop.
This does what you want:

Python code:
import random

class Node:
    def __init__(self, r):
        self.routes = r
        self.hopcount = 0

def nexthop(routes):
    return routes[random.randrange(len(routes))]

node1 = Node([2])
node2 = Node([1,3,4,5])
node3 = Node([2,4,5])
node4 = Node([2,3,5])
node5 = Node([2,3,4,6])
node6 = Node([5])

noderoutes = [node1.routes, node2.routes, node3.routes,
    	      node4.routes, node5.routes, node6.routes]

while True:
    for noderoute in noderoutes:
        print(nexthop(noderoute))
    print('end')
It will print random number each time.

You can also randomize initial nodes with something like that:
Python code:
maxCount = 10
noderoutes = []
for x in xrange(6):
    node = Node(sorted(random.sample(range(1, maxCount),
                random.randint(1, maxCount-1))))
    noderoutes.append(node)
With time you will purify.

evilentity fucked around with this message at 19:51 on Feb 14, 2013

evilentity
Jun 25, 2010

Wildtortilla posted:

I'm currently taking PSU's Certification in Geographic Information Systems (aka, intelligently using ArcMAP). In May I'll be starting my final course for the certificate and I have a huge array of options, but I'm leaning towards their course in Python. From looking at job postings for GIS positions, I'd wager at least 50% of postings include knowing Python as a desirable skill. However, coming from a background in geology, I have no experience with coding; would the links in the OP be a good place for me to start or should I start elsewhere since I have no experience? The first link in the tutorials "MIT Introduction to Computer Science and Programming" and the contents of this post seem like they'd be a good start for me. Any suggestions?

You could try
http://learnpythonthehardway.org/
or
https://www.edx.org/courses/MITx/6.00x/2013_Spring/about
Intro to CS in python. I did previous one and it was pretty fun, but difficulty ramps up quickly after few lectures.
Im sure people around here have other suggestions.

Python is pretty easy to get into, so with enough determination you will get it.

evilentity
Jun 25, 2010

Mad Pino Rage posted:

I was doing pretty good as in I was writing something. The assignment is to write a main function to call functions for A) list a deck of cards in 52 rows and 3 columns: a number given to each card(0-51), the card itself, and show the location of the card(deck player's hand, computer's hand) B)clear the deck so all cards start in the deck C)assign five cards to each the player and computer D) show the cards in the player's hand and the computer's hand

I was able to write something that showed the list, and then I got lost in writing a way to assign cards to the player.

I was bored so I did this for you. Try to use this as guidelines for your own work and dont just copy verbatim. At least try to understand why and how it does things. Ive commented things that I thought might be hard. There is no error checking so you could add that.

Python code:
import random

DECK_ID = 0
PLAYER_ID = 1
COMP_ID = 2

SUITS = ("hearts", "diamonds", "spades", "clubs")
RANKS = ("Ace", "Two", "Three", "Four", "Five", "Six", "Seven",
            "Eight", "Nine", "Ten", "Jack", "Queen", "King")

def create_deck():
    deck = []
    for suit in SUITS:
        for rank in RANKS:
            deck.append([rank, suit, DECK_ID])
    return deck

def clear_deck(deck):
    # change id in card to deck
    for card in deck:
        card[2] = DECK_ID
   
def print_deck(deck):
    # print simple header
    print ' ID  CARD               LOCATION'
    for i, card in enumerate(deck):
        # create string with cards name
        card_name = card[0] + ' of ' + card[1] 
        # create columns, :>3 pads string to len 3
        print '{id:>3}  {card:<17}  {deck}'.format(id=i, card=card_name, deck=card[2])
    # footer
    print ' ID  CARD               LOCATION'

def new_hand(deck, id):
    # create list with cards in deck that are not in other hands
    available_cards = [x for x in deck if x[2] == 0]
    # get 5 random cards from them
    hand = random.sample(available_cards, 5)
    # cards int hand are references for cards in deck
    # so changing them affects deck as well
    for card in hand:
        card[2] = id
    return hand

def print_hand(hand):
    for card in hand:
        print card
    
def main():
    deck = create_deck()
    player_hand = new_hand(deck, PLAYER_ID)
    print_hand(player_hand)
    comp_hand = new_hand(deck, COMP_ID)
    print_hand(comp_hand)
    
    print_deck(deck)
    clear_deck(deck)
    print_deck(deck)
        
if __name__ == '__main__':
    main()
Feel free to improve this.

evilentity
Jun 25, 2010

ARACHNOTRON posted:

why is Python so weird??

What if I did something with site or added the main package location to sys.path in every module? I don't really want to dick around with PYTHONPATH right now because I'm only doing test stuff.

What is so weird about it? It will search folder the file is in, subfolders, path and libs. It doesnt magically know wheres your stuff located. Try structuring your project in different way. Path is hardly magic. If you really must you can use this:
Python code:
import imp
foo = imp.load_source('module.name', '/path/to/file.py')
via SO

Mad Pino Rage posted:

Write a loop that traverses:

code:
['spam!', 1, ['Brie', 'Roquefort', 'Pol le Veq'], [1, 2, 3]]
and prints the length of each element. What happens if you send an integer to len? Change 1 to 'one' and run your solution again.

Python code:
testlist = ['spam!', 'one', ['Brie', 'Roquefort', 'Pol le Veq'], ['1', '2', '3']]

for item in testlist:
    print len(item)
...
Unless you need the index for something other than acces you dont need to enumerate. Are you sure you have to do the same for inner lists?

Mad Pino Rage posted:

Open a file named ch09e02.py and with the following content:

I'm run it through the interpreter and I get this:

*** DocTestRunner.merge: '__main__.test' in both testers; summing outcomes.
*** DocTestRunner.merge: '__main__' in both testers; summing outcomes.

Now I'll go through the examples in the chapter listed above and get it to say that it's passed(at least I remember doing so about an hour ago), so why am I getting this now?
Did you forget -v after your script for verbose output?

Mad Pino Rage posted:

Well, I wrote this to pass a doctest in one of the exercises, but I don't understand a piece of code. I actually copied it from another project I wrote earlier in this semester.
list3 = [i for i in u]
I don't know how to translate i for i in u in an English statement. I know what it does, but I don't know how to say it if I were having a conversation with someone. I get to the assignment to list3 is the list with as many indices... and then I can't really think of how to say it.

Thanks for your help!
Its a list comprehension. [i for i in u] could be written more clearly like this: [(do) x for (each) item in list].

evilentity
Jun 25, 2010

Dominoes posted:

Here's the read_cell function:
Python code:
def read_cell(x, y, file_name):
#Returns a cell value in a csv
    with open(file_name, 'r') as f:
        myreader = reader(f)
        y_count = 0
        for n in myreader:
            if y_count == y:
                cell = n[x]
                return cell
            y_count += 1

And thats the reason why its so slow. Read the whole file into memory and go from there. I assume reader is csv.reader. Something like that should do:
Python code:
def load_csv(file_name):
    with open(file_name, 'r') as f:
        return list(reader(f))
csv_data = load_csv(...)

print dsv_data[x][y]
Edit: beaten into the ground.

evilentity
Jun 25, 2010

Dominoes posted:

Another CSV / memory vs drive write question. If I want to download a CSV from a url, is there a way I can put the file information directly into a variable in memory? I've tried many combinations using list(), read(), csv.reader() etc, but can't find a working solution that skips writing the csv to disk, and reading it.

Code like this works:
Python code:
    def function():
        u = urlopen('http://somethingawful.com/COBOL.csv')
        with open('drive_file.csv', 'wb') as f:
            f.write(u.read())
        
        with open('drive_file.csv', 'r') as f:
            return list(csv.reader(f))
For example, this seems like it should do what I'm asking, but doesn't.

Python code:
    def function():
        u = urlopen('http://somethingawful.com/COBOL.csv')
        return list(csv.reader(u.read()))

It doesnt work because reader expects file handle not string.

Maybe you can use StringIO to makes this easy like this:
Python code:
import StringIO    
def function():
    u = urlopen('http://somethingawful.com/COBOL.csv').read()
    return list(csv.reader(StringIO.StringIO(u)))
Ive never used this module so i dunno. Maybe theres better way.

evilentity
Jun 25, 2010

dantheman650 posted:

I added the Python directory to PATH and am still getting that error if I first don't CD to my programs directory, but that's ok - it only takes a second to CD and that works fine for now.

I'm going to stop cluttering the thread now and go practice!

You added PYTHONPATH so you can type 'python' in command line. When you do 'python hello.py' cmd knows wheres python, but not hello.py. Thats why you need to cd to your script path first. Alternatively, you can specify full path like so: 'python c:\dev\hello.py'.

You can do [ code=python ] for proper python syntax highlighting.

evilentity fucked around with this message at 23:41 on May 1, 2013

evilentity
Jun 25, 2010
I did a thing:
https://github.com/piotr-j/wf_alerts

Simple alerts about tweets with specific keywords. Windows only due to winsound. Do your worst.

evilentity
Jun 25, 2010

DSA_Key posted:

So I needed something that could execute commands on multiple Linux systems and bring the data back to me for parsing. So using Python and Pexpect I wrote the following and I just wanted to share as this is the very first project I've ever undertaken in Python let alone Pexpect. The only thing the script doesn't ask for is the file with the IP addresses or hostnames instead it's hard coded to read a targets.txt file in the directory it's executed from.

Eventually I'll update it so it has a help function and ask for a custom file with a list of hosts and maybe sanitize user input etc.... But I was stoked I got this far and I just want to share! Python's pretty cool.

edit* also it writes an error log with a best guess as to why it couldn't log into a server.


Looks reasonable for most part. Here are few tips:

When you use `with` you dont need to close the file.
Use logging module for logging.
In Python empty string is considered false, thus `if host != "":` is equal to `if host:`.
In the `if i == 0:` etc try not to using magic numbers. Just make a constant at the top like `E_TIMED_OUT = 0`
Also try using some more meaningful variable names, i's and j's arent very clear.

Python's pretty cool!

evilentity
Jun 25, 2010

digitalcamo posted:

Something I've been thinking about while learning Python, is it possible to write iPhone apps using Python? That idea seemed pretty neat as I'm trying to figure out what kind of programming I'd like to pursue. Followed the advice of others and really enjoy Codecademy. Turns out it is not a total waste of time like some of the reviews I read before joining.

I havent used it, but Kivy might interest you: http://kivy.org/
Its a multiplatform framework for various systems including ios.

evilentity
Jun 25, 2010

Rocko Bonaparte posted:

On that token, are there patterns you'd see in Python you wouldn't see in your typical statically-typed enterprise language?

(At this point I've given up trying to rationalize what I see a lot here. I think they might just be on drugs.)

Unless module is very simple, there really shouldnt be global state in it. Classes arent as ubiquitous as they are in, say, java.
Often you can get away with a bunch of functions. Thinks statics in java.
If you really need some state, you can nest a bunch of functions as well. But i wouldnt go over board with that.

Can we some examples? Maybe anonymize it a litte, if you cant post verbatim.

evilentity
Jun 25, 2010

NtotheTC posted:

Python code:
	team_combos = list(combinations(self.player_list, 5))

Already give you all unique combinations. Whats your question again?

evilentity
Jun 25, 2010

Goonicus posted:

Just getting into learning, right now going through the Learn Python the Hard Way course.

My question right now is for the first language beginners, what was your first project in Python? Did you have a goal in mind for something you wanted to accomplish?

When i started learning python 2 years ago, ive wrote pretty big gui application for company i work for. That was a huge mistake. Wayyy too complicated for first project. Im refactoring it now :v:

Your best bet is to try to fix an itch of yours. Automate some mundane task, create a blog. Something reasonably complex, but not too much. Go nuts and learn.

evilentity
Jun 25, 2010

hemophilia posted:

I have a problem where I'm supposed to take the data inside two dictionaries, and multiply them. The conciet of the lesson is that I'm supposed to multiply the 'stock' of my fruit and veggies by their cost for a total potential profit. The website/interpreter keeps spitting back that I'm not returning the right numbers but everything looks right to me, so I'm assuming it wants me to add all of it together for a grand total.

print is not return.

evilentity
Jun 25, 2010

FoiledAgain posted:

What's this about?

code:
>>> type(None)
<class 'NoneType'>
>>> isinstance(None, NoneType)
Traceback (most recent call last):
  File "<string>", line 301, in runcode
  File "<interactive input>", line 1, in <module>
NameError: name 'NoneType' is not defined
How can it tell me that None is of type NoneType, and then tell me it doesn't know what NoneType is?

Why the hell do you need this?

evilentity
Jun 25, 2010

Pollyanna posted:

Is there a version of this for PySide? I'm looking to start a GUI version of my stocks app and I like this book, but it's only for PyQt.

PySide and PyQt are fairly similar. Bigger issues is that this book is over 6 years old. Ill check it out, hopefully general concept didnt change all that much.

evilentity
Jun 25, 2010

kalstrams posted:

Windows 8.1 x64. How do I make Python 2.7 run from cmd ?

Python 2.7 is installed on C:/Python27 (from python(x,y)).
I have tried:
  • Powershell "[Environment]::SetEnvironmentVariable("Path", "$env:Path;C:\Python27", "User")"
  • Adding Python to Windows PATH (both through the Control Panel and terminal).
  • Copying a python.bat file with "@C:\Python27\python.exe %*" to the system32.
Both "python" and "call python" give me "is not recognized bla bla...".

User variable Path: http://privatepaste.com/0564253fc5
System variable Path: http://privatepaste.com/cd39597e02

Adding C:\python27; to path should be sufficient. Did you restart the cmd prompt? Try instaling normal python in other folder. Maybe the packadge is hosed.

evilentity
Jun 25, 2010

keyvin posted:

My program is corrupting files it copies, and I can't figure out why. I am using the OS module to compare time stamps and copy the files. I do open the files, but only for reading.

Code here

Its just a stupid script I wrote to be a ghetto steam cloud using google drive. I know blindly loading random files into memory is bad if those files are too large, but this is just a prototype.

I did something similar, basically mirror stuff to and from a pendrive.
This should do the trick:
Python code:
distutils.dir_util.copy_tree(src, dst, update=1)  
Your mp5 is maybe wrong.
Python code:
def md5_of_file(path, block_size=2**20):
    md5 = hashlib.md5()
    with open(path, 'rb') as f: 
        for chunk in iter(lambda: f.read(128*md5.block_size), ''): 
             md5.update(chunk)
    return md5.digest()

evilentity
Jun 25, 2010

fletcher posted:

How come these produce different results?

code:
>>> from datetime import datetime
>>> from pytz import timezone
>>> timezone_la = timezone('America/Los_Angeles')
>>> datetime(2014, 4, 2, 23, 30, tzinfo=timezone_la)
datetime.datetime(2014, 4, 2, 23, 30, tzinfo=<DstTzInfo 'America/Los_Angeles' PST-1 day, 16:00:00 STD>)
>>> timezone_la.localize(datetime(2014, 4, 2, 23, 30))
datetime.datetime(2014, 4, 2, 23, 30, tzinfo=<DstTzInfo 'America/Los_Angeles' PDT-1 day, 17:00:00 DST>)
Python code:
>>> timezone_la = timezone('America/Los_Angeles')
>>> tz = timezone_la.localize(datetime(2014, 4, 2, 23, 30))
>>> dt = datetime(2014, 4, 2, 23, 30, tzinfo=timezone_la)
>>> timezone_la.dst
<bound method America/Los_Angeles.dst of <DstTzInfo 'America/Los_Angeles' PST-1
day, 16:00:00 STD>>
>>> tz 
datetime.datetime(2014, 4, 2, 23, 30, tzinfo=<DstTzInfo 'America/Los_Angeles' PD
T-1 day, 17:00:00 DST>)
>>> dt 
datetime.datetime(2014, 4, 2, 23, 30, tzinfo=<DstTzInfo 'America/Los_Angeles' PS
T-1 day, 16:00:00 STD>)
I guess __repr__ of DstTzInfo accounts for daylight savings time?

Looks like it
Python code:
    def __repr__(self):
        if self._dst:
            dst = 'DST'
        else:
            dst = 'STD'
        if self._utcoffset > _notime:
            return '<DstTzInfo %r %s+%s %s>' % (
                    self.zone, self._tzname, self._utcoffset, dst
                )
        else:
            return '<DstTzInfo %r %s%s %s>' % (
                    self.zone, self._tzname, self._utcoffset, dst
                )
source

evilentity
Jun 25, 2010

Akarshi posted:

Okay guys, I'm super new to Python and I think this question is Python related. If it's not, though, feel free to point me in the right place.

I'm trying to figure out how to compare the contents of two Google Protocol Buffer messages. So I have these two messages, and they have a lot of different values that sometimes drill down to more values (for example, I have a Message that has a string, an int, and a custom_snapshot, where custom_snapshot is comprised of an int, a string, and so on). I want to see if these two messages are the same. I don't want to compare each value one by one, since that will take a while, so I was wondering if there was a quick way to do this in Python?

I tried doing messageA.debugString() == messageB.debugString(), but apparently there is no debugString method that I could access when I tried. Any help would be appreciated. Thanks!

You sure you cant just do messageA == messageB?
If you have custom messages you need to implement __eq__ magic method.

Adbot
ADBOT LOVES YOU

evilentity
Jun 25, 2010
Also dont start instance variable name with Capital letter.

  • Locked thread