Search Results

Search found 15187 results on 608 pages for 'boost python'.

Page 82/608 | < Previous Page | 78 79 80 81 82 83 84 85 86 87 88 89  | Next Page >

  • Python Closures Example Code

    - by user336527
    I am learning Python using "Dive Into Python 3" book. I like it, but I don't understand the example used to introduce Closures in Section 6.5. I mean, I see how it works, and I think it's really cool. But I don't see any real benefit: it seems to me the same result could be achieved by simply reading in the rules file line by line in a loop, and doing search / replace for each line read. Could someone help me to: either understand why using closures in this example improves the code (e.g., easier to maintain, extend, reuse, or debug?) or suggest a source of some other real-life code examples where closures really shine? Thank you!

    Read the article

  • Match groups in Python

    - by Curd
    Is there a way in Python to access match groups without explicitely creating a match object (or another way to beautify the example below)? Here is an example to clarify my motivation for the question: Following perl code if ($statement =~ /I love (\w+)/) { print "He loves $1\n"; } elsif ($statement =~ /Ich liebe (\w+)/) { print "Er liebt $1\n"; } elsif ($statement =~ /Je t\'aime (\w+)/) { print "Il aime $1\n"; } translated into Python m = re.match("I love (\w+)", statement) if m: print "He loves",m.group(1) else: m = re.match("Ich liebe (\w+)", statement) if m: print "Er liebt",m.group(1) else: m = re.match("Je t'aime (\w+)", statement) if m: print "Il aime",m.group(1) looks very awkward (if-else-cascade, match object creation).

    Read the article

  • Inserting multiple types into an SQLite database with Python

    - by mankoff
    I'm trying to create an SQLite 3 database from Python. I have a few types I'd like to insert into each record: A float, and then 3 groups of n floats, currently a tuple but could be an array or list.. I'm not well-enough versed in Python to understand all the differences. My problem is the INSERT statement. DAS = 12345 lats = (42,43,44,45) lons = (10,11,12,13) times = (1,2,3,4,5,6,7,8,9) import sqlite3 connection = sqlite3.connect("test.db") cursor = connection.cursor() cursor.execute( "create table foo(DAS LONG PRIMARY KEY,lats real(4),lons real(4), times real(9) )" ) I'm not sure what comes next. Something along the lines of: cmd = 'INSERT into foo values (?,?,?,?), ..." cursor.execute(cmd) How should I best build the SQL insert command given this data?

    Read the article

  • simple python file writing question

    - by aharon
    I'm learning Python, and have run into a bit of a problem. On my OSX install of Python 3.1, this happens in the console: >>> filename = "test" >>> reader = open(filename, 'r') >>> writer = open(filename, 'w') >>> reader.read() '' >>> writer.write("hello world\n") 12 >>> reader.read() '' And calling more test in BASH confirms that there is nothing in test. What's going on? Thanks.

    Read the article

  • TypeError: Python thinks that I passed a function 2 arguments but I only passed it 1

    - by slhck
    I work on something in Seattle Repy which is a restricted subset of Python. Anyway, I wanted to implement my own Queue that derives from a list: class Queue(list): job_count = 0 def __init__(self): list.__init__(self) def appendleft(item): item.creation_time = getruntime() item.current_count = self.job_count self.insert(0, item) def pop(): item = self.pop() item.pop_time = getruntime() return item Now I call this in my main server, where I use my own Job class to pass Jobs to the Queue: mycontext['queue'] = Queue() # ... job = Job(str(ip), message) mycontext['queue'].appendleft(job) The last line raises the following exception: Exception (with type 'exceptions.TypeError'): appendleft() takes exactly 1 argument (2 given) I'm relatively new to Python, so could anyone explain to me why it would think that I gave appendleft() two arguments when there obviously was only one?

    Read the article

  • Python doesn't work properly when I execute a script after using Right Click >> Command Prompt Here

    - by Dominic Bou-Samra
    This is a weird bug. I know it's something funky going on with my PATH variable, but no idea how to fix it. If I have a script C:\Test\test.py and I execute it from within IDLE, it works fine. If I open up Command Prompt using Runcmd.exe and navigate manually it works fine. But if I use Windows 7's convenient Right Click on folder Command Prompt Here then type test.py it fails with import errors. I also cannot just type "python" to reach a python shell session if I use the latter method above. Any ideas?

    Read the article

  • what does the '~' mean in python?

    - by hidroto
    what does the '~' mean in python? i found this BF interpreter in python a while ago. import sys #c,i,r,p=0,0,[0]*255,raw_input() c=0 i=0 p=raw_input() r=[0]*255 while c<len(p): m,n,u=p[c],0,r[i] if m==">":i+=1 if m=="<":i-=1 if m=="+":r[i]+=1 if m=="-":r[i]-=1 if m==".":sys.stdout.write(chr(u)) if m=="[": if ~u: while 1: m=p[c] if m=="]":n-=1 if m=="[":n+=1 if ~n:break c+=1 if m=="]": if u: while 1: m=p[c] if m=="]":n-=1 if m=="[":n+=1 if ~n:break c-=1 c+=1 and i want to know what it does because i want to make one on my ti 84 (and a PF one) BF is http://en.wikipedia.org/wiki/Brainfuck and PF is something similar

    Read the article

  • Determing Python version at runtime

    - by Gekitsuu
    The problem I'm having is that I've written code that depends on features in python2.6 so I'm trying to build a check that will produce a useful error if someone tries to run it on a machine with python lower than 2.6. The code I put in to check for the specific version and match it should work fine but the problem I have is the interpreter on a 2.4 machine reads the file and sees a "finally" at the end of my try loop and errors out before it gets far enough into execution for my if statement to handle the version mismatch. Other than changing the magic line to specifically look for /usr/bin/python2.6 is there a way python can handle this sort of thing?

    Read the article

  • Python: using a regular expression to match one line of HTML

    - by skylarking
    This simple Python method I put together just checks to see if Tomcat is running on one of our servers. import urllib2 import re import sys def tomcat_check(): tomcat_status = urllib2.urlopen('http://10.1.1.20:7880') results = tomcat_status.read() pattern = re.compile('<body>Tomcat is running...</body>',re.M|re.DOTALL) q = pattern.search(results) if q == []: notify_us() else: print ("Tomcat appears to be running") sys.exit() If this line is not found : <body>Tomcat is running...</body> It calls : notify_us() Which uses SMTP to send an email message to myself and another admin that Tomcat is no longer runnning on the server... I have not used the re module in Python before...so I am assuming there is a better way to do this... I am also open to a more graceful solution with Beautiful Soup ... but haven't used that either.. Just trying to keep this as simple as possible...

    Read the article

  • Python sys.argv lists and indexes

    - by Fred Gerbig
    In the below code I understand that sys.argv uses lists, however I am not clear on how the index's are used here. def main(): if len(sys.argv) >= 2: name = sys.argv[1] else: name = 'World' print 'Hello', name if __name__ == '__main__': main() If I change name = sys.argv[1] to name = sys.argv[0] and type something for an argument it returns: Hello C:\Documents and Settings\fred\My Documents\Downloads\google-python-exercises \google-python-exercises\hello.py Which kind of make sense. Can someone explain how the 2 is used here: if len(sys.argv) >= 2: And how the 1 is used here: name = sys.argv[1]

    Read the article

  • What are the common programming mistakes in Python?

    - by Paul McGuire
    I was about to tag the recent question in which the OP accidentally shadowed the builtin operator module with his own local operator.py with the "common-mistakes" tag, and I saw that there are a number of interesting questions posted asking for common mistakes to avoid in Java, Ruby, Scala, Clojure, .Net, jQuery, Haskell, SQL, ColdFusion, and so on, but I didn't see any for Python. For the benefit of Python beginners, can we enumerate the common mistakes that we have all committed at one time or another, in the hopes of maybe steering a newbie or two clear of them? (In homage to "The Princess Bride", I call these the Classic Blunders.) If possible, a little supporting explanation on what the problem is, and the generally accepted resolution/workaround, so that the beginning Pythoner doesn't read your answer and say "ok, that's a mistake, how do I fix it?"

    Read the article

  • Connection to DB2 in Python

    - by Mestika
    Hi, I'm trying to create a database connection in a python script to my DB2 database. When the connection is done I've to run some different SQL statements. I googled the problem and has read the ibm_db API (http://code.google.com/p/ibm-db/wiki/APIs) but just can't seem to get it right. Here is what I got so far: import sys import getopt import timeit import multiprocessing import random import os import re import ibm_db import time from string import maketrans query_str = None conn = ibm_db.pconnect("dsn=write","usrname","secret") query_stmt = ibm_db.prepare(conn, query_str) ibm_db.execute(query_stmt, "SELECT COUNT(*) FROM accounts") result = ibm_db.fetch_assoc() print result status = ibm_db.close(conn) but I get an error. I really tried everything (or, not everything but pretty damn close) and I can't get it to work. I just need to make a automatic test python script that can test different queries with different indexes and so on and for that I need to create and remove indexes a long the way. Hope someone has a solutions or maybe knows about some example codes out there I can download and study. Thanks Mestika

    Read the article

  • Must See Conference Videos for Python/Django Developers

    - by Koobz
    There's lots of good conference videos online regarding Python and Django development. Instead of watching ST:TNG at the computer, I figure it'd more productive to hone my knowledge . Fire away with some of your most inspiring and educational Python, Django, or simply programming related talks. Provide an explanation of why you found the talk useful. Examples: James Bennet on Re-usable Apps - Got me to take a serious look at django apps. Put together a fairly robust site in two days afterwards with django-cms, django-photologue, django-contact-form. Good advice on when your app is crossing boundaries and why it's good to err on the site of 'make it a separate app.'

    Read the article

  • Resources for TDD aimed at Python Web Development

    - by Null Route
    I am a hacker not and not a full-time programmer but am looking to start my own full application development experiment. I apologize if I am missing something easy here. I am looking for recommendations for books, articles, sites, etc for learning more about test driven development specifically compatible with or aimed at Python web application programming. I understand that Python has built-in tools to assist. What would be the best way to learn about these outside of RTFM? I have searched on StackOverflow and found the Kent Beck's and David Astels book on the subject. I have also bookmarked the Wikipedia article as it has many of these types of resources. Are there any particular ones you would recommend for this language/application?

    Read the article

  • Python How to make a cross-module function?

    - by Evan
    I want to be able to call a global function from an imported class, for example In file PetStore.py class AnimalSound(object): def __init__(self): if 'makenoise' in globals(): self.makenoise = globals()['makenoise'] else: self.makenoise = lambda: 'meow' def __str__(self): return self.makenoise() Then when I test in the Python Interpreter >>> def makenoise(): ... return 'bark' ... >>> from PetStore import AnimalSound >>> sound = AnimalSound() >>> sound.makenoise() 'meow' I get a 'meow' instead of 'bark'. I have tried using the solutions provided in python-how-to-make-a-cross-module-variable with no luck.

    Read the article

  • Why the "mutable default argument fix" syntax is so ugly, asks python newbie

    - by Cawas
    Now following my series of "python newbie questions" and based on another question. Go to http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html#other-languages-have-variables and scroll down to "Default Parameter Values". There you can find the following: def bad_append(new_item, a_list=[]): a_list.append(new_item) return a_list def good_append(new_item, a_list=None): if a_list is None: a_list = [] a_list.append(new_item) return a_list So, question here is: why is the "good" syntax over a known issue ugly like that in a programming language that promotes "elegant syntax" and "easy-to-use"? Why not just something in the definition itself, that the "argument" name is attached to a "localized" mutable object like: def better_append(new_item, a_list=[].local): a_list.append(new_item) return a_list I'm sure there would be a better way to do this syntax, but I'm also almost positive there's a good reason to why it hasn't been done. So, anyone happens to know why?

    Read the article

  • Multiple classes in a Python module

    - by ralphL
    I'm very new to Python (I'm coming from a JAVA background) and I'm wondering if anyone could help me with some of the Python standards. Is it a normal or "proper" practice to put multiple class in a module? I have been working with Django and started with the tutorials and they place their database model classes in the same module. Is this something that is normally done or should I stick with 1 class per module? Is their a reason I would do one over the other? Hope I'm being clear and not to generic. Thanks to everyone in advance!

    Read the article

  • what is the proper way to do logging in csv file?

    - by user2003548
    i want to log some information of every single request send to a busy http server in a formatted form,use log module would create some thing i don't want to: [I 131104 15:31:29 Sys:34] i think of csv format but i don't know how to customize it,and python got csv module,but read the manual import csv with open('some.csv', 'w', newline='') as f: writer = csv.writer(f) writer.writerows(someiterable) since it would open and close a file each time, i am afraid in this way would slow down the whole server performance, what could i do?

    Read the article

  • Python urllib3 and how to handle cookie support?

    - by bigredbob
    So I'm looking into urllib3 because it has connection pooling and is thread safe (so performance is better, especially for crawling), but the documentation is... minimal to say the least. urllib2 has build_opener so something like: #!/usr/bin/python import cookielib, urllib2 cj = cookielib.CookieJar() opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) r = opener.open("http://example.com/") But urllib3 has no build_opener method, so the only way I have figured out so far is to manually put it in the header: #!/usr/bin/python import urllib3 http_pool = urllib3.connection_from_url("http://example.com") myheaders = {'Cookie':'some cookie data'} r = http_pool.get_url("http://example.org/", headers=myheaders) But I am hoping there is a better way and that one of you can tell me what it is. Also can someone tag this with "urllib3" please.

    Read the article

< Previous Page | 78 79 80 81 82 83 84 85 86 87 88 89  | Next Page >