Does anyone know of a memory efficient way to generate very large xml files (e.g. 100-500 MiB) in Python?
I've been utilizing lxml, but memory usage is through the roof.
Suppose user enter this string at terminal
123 456 456 //then hit enter
How do I scan these three (could be more) numbers in different variables in python
Could be something like this:
for i in range(1,n)
m[i]=#WHAT FUNCTION SHOULD I PUT HERE
In c++ we can easily use cin>>m[i] inside above loop to scan the variables.
If i use input() or raw_input() , they would scan whole line in single variable.
Hi,
Fastest way to uniqify a list in Python without preserving order? I saw many complicated solutions on Internet - could they be faster then simply:
list(set([a,b,c,a]))
?
In Javascript I can do this:
function A(x) { return x || 3; }
This returns 3 if x is a "non-truthful" value like 0, null, false, and it returns x otherwise. This is useful for empty arguments, e.g. I can do A() and it will evaluate as 3.
Does Python have an equivalent? I guess I could make one out of the ternary operator a if b else c but was wondering what people use for this.
I write a python class which makes asynchronous method calls using D-Bus. When my reply_handler is called, it stores data in list. This list can be used by another class methods at the same time. Is it safe or I can use only synchronized data structures like Queue class?
Format is like:
CHINA;2002-06-25 00:00:00.000;5,60
CHINA;2002-06-26 00:00:00.000;5,32
CHINA;2002-06-27 00:00:00.000;5,31
and I try to use Python's CSV tools to parse it but cannot understand the paragraph, source:
And while the module doesn’t directly support parsing strings, it can easily be done:
import csv
for row in csv.reader(['one,two,three']):
print row
Could someone clarify the line ['one,two,three']? How would you use it with format A;B;C?
I am trying to get the response codes from Mechanize in python. While I am able to get a 200 status code anything else isn't returned (404 throws and exception and 30x is ignored). Is there a way to get the original status code?
Thanks
I have a question about idioms and readability, and there seems to be a clash of Python philosophies for this particular case:
I want to build dictionary A from dictionary B. If a specific key does not exist in B, then do nothing and continue on.
Which way is better?
try:
A["blah"] = B["blah"]
except KeyError:
pass
or
if "blah" in B:
A["blah"] = B["blah"]
"Do and ask for forgiveness" vs. "simplicity and explicitness".
Which is better and why?
Hi
I wrote a matlab code (that easily could be implimented as a function) that convert a series of BMPs to avi. I want a python program to call to this program/function. how do I do it?
thanks
I am pretty new to python and I discovered by myself that I can create a list of function and call with a for loop.
example:
def a(args):
print "A"
def b(args):
print "B"
def c(args):
print "C " + str(args)
functions = [a,b,c]
for i in functions:
i(1)
So, my question is: is there any good practice or elegant way to use list of functions and what is a good use of all this? (do have a particular name the "list of functions"?)
thank you
The greenlet package is used by gevent and eventlet for asynchronous IO. It is written as a C-extension and therefore doesn't work with Jython or IronPython. If performance is of no concern, what is the easiest approach to implementing the greenlet API in pure Python.
A simple example:
def test1():
print 12
gr2.switch()
print 34
def test2():
print 56
gr1.switch()
print 78
gr1 = greenlet(test1)
gr2 = greenlet(test2)
gr1.switch()
Should print 12, 56, 34 (and not 78).
Hey, here's a dumb question: how can I set an object property given its name in a string. I have a dictionary being passed to me and I wish to transfer its values into namesake properties using code like this:
for entry in src_dict:
if entry.startswith('can_'):
tgt_obj[entry] = src_dict_profile[entry]
I'm still a bit of a noob with Python so would appreciate some help.
- dave.
Hi,
I need to compare two files and redirect the different lines to third file. I know using diff command i can get the difference . But, is there any way of doing it in python ? Any sample code will be helpful
I'm looking for a way in python to find out which type of file system is being used for a given path. I'm wanting to do this in a cross platform way. On linux I could just grab the output of df -T but that won't work on OSX or windows.
Hi folks,
I'm having quite a problem deciding how to serve a few Python scripts.
The problem is that the basic functionality could be generalized by this:
do_something()
time.sleep(3)
do_something()
I tried various WSGI servers, but they have all been giving me concurrency limitations, as in I have to specify how many threads to use and so on.
I only wish that the resources on the server be used efficiently and liberally.
Any ideas?
Hi
I am trying to perform a 2d convolution in python using numpy
I have a 2d array as follows with kernel H_r for the rows and H_c for the columns
data = np.zeros((nr, nc), dtype=np.float32)
#fill array with some data here then convolve
for r in range(nr):
data[r,:] = np.convolve(data[r,:], H_r, 'same')
for c in range(nc):
data[:,c] = np.convolve(data[:,c], H_c, 'same')
It does not produce the output that I was expecting, does this code look OK
Thanks
I downloaded a webpage in my python script.
In most cases, this works fine.
However, this one had a response header: GZIP encoding, and when I tried to print the source code of this web page, it had all symbols in my putty.
How do decode this to regular text?
Hello everyone, i'm looking for a way in python to run an external binary and watch it's output for: "up to date" If "up to date" isn't returned i want to run the original command again, once "up to date" is displayed i would like to be able to run another script. So far I've figured out how to run the binary with options using subprocess but thats as far as I've gotten. Thanks!
Is there some relatively simple way to programmatically include source code lines to python logger report. For example...
import logging
def main():
something_is_not_right = True
logging.basicConfig(level=logging.DEBUG,
format=('%(filename)s: '
'%(levelname)s: '
'%(funcName)s(): '
'%(lineno)d:\t'
'%(message)s')
)
if something_is_not_right == True:
logging.debug('some way to get previous line of source code here?')
So that output would look like this.
example.py: DEBUG: main(): 14: if something_is_not_right == True:
Given a PyObject* pointing to a python object, how do I invoke one of the object methods? The documentation never gives an example of this:
PyObject* obj = ....
PyObject* args = Py_BuildValue("(s)", "An arg");
PyObject* method = PyWHATGOESHERE(obj, "foo");
PyObject* ret = PyWHATGOESHERE(obj, method, args);
if (!ret) {
// check error...
}
This would be the equivalent of
>>> ret = obj.foo("An arg")
I'm trying to use Emacs as a python editor and it works fine when I evaluate(C-c C-c) only single files but when I evaluate a file that imports another file in the same directory, I get an error saying that the file could not be imported.
Does anyone know of a workaround?
Thanks in advance
I'd like to work with a dict in python, but limit the number of key/value pairs to X. In other words, if the dict is currently storing X key/value pairs and I perform an insertion, I would like one of the existing pairs to be dropped. It would be nice if it was the least recently inserted/accesses key but that's not completely necessary.
If this exists in the standard library please save me some time and point it out!