2,646 Posted Topics
Re: It is nice, but I think echo() should take a single argument and return a single argument like this [code=python] def echo(arg): print(arg) return arg [/code] Having a unary function allows use with imap() for example [code=python] from itertools import imap s = sum(imap(echo, (x * x for x in … | |
Re: Use [icode]os.path.basename(path)[/icode] to get the filename from the path. | |
Re: This is Cython code. It seems to be a version of Prahaai's pure python code here [url]http://www.daniweb.com/software-development/python/threads/152831[/url]. See if the pure python version works. Also notice that a pyPdf module exists here [url]http://pybrary.net/pyPdf/[/url] . Here is an example code to count the pages in a pdf file with python 2 … | |
Re: It looks easy, replace all the v1, v2, v3 .. variables with arrays v[0], v[1], v[2], something like [code=python] import tkinter as tk from functools import partial def klik(n): button[n].config(image=s[n]) root = tk.Tk() frame1 = tk.Frame(root) frame1.pack(side=tk.TOP, fill=tk.X) karirano = tk.PhotoImage(file="kari.GIF") s = list(tk.PhotoImage(file="%d.GIF" % (i+1)) for i in range(4)) … | |
Re: Your pseudo code is cryptic. In the 2 first ifs, row seems to be an integer, then in the else part it's an iterable, and you don't say what 'the first in row' and 'the last in row mean'. Could you give an example of what the row variable contains … | |
Re: When one of c or d is not zero, the equation has an infinite number of solutions. In fact the solutions of [icode]x * c + y * d = f[/icode] are [code=text] x = (f * c - z * d)/(c**2 + d**2) y = (f * d + … | |
Re: What about copyfile(), copy() and copy2() in module shutil ? | |
Re: You could use the format method [code=python] def __str__(self): return "Dog named '{0}', aged {1} (human age {2} years)".format( self.name, self.age, self.getPersonAge()) [/code] Also the class name should be capitalized (Dog) to conform python's tradition. | |
Re: [QUOTE=xopenex;1766164]hello, how would i change the code here, to have python create variables that i can call later, instead of files?[/QUOTE] The best way is to create a dictionary instead of variables [code=python] from pprint import pprint dic = dict() for a in range(50): if a % 10 in (4, … | |
Re: Try [code=python] return "".join((seqA, '\n', a, '\n', seqB, '\n', 'Score: %d' % sum(c))) [/code] edit: don't use tabs to indent python code, use 4 spaces. | |
Re: No it's not possible to have assignments in if conditions. The reason is that assignment is a statement and cannot be part of an expression (the same applies to += -=, etc), while the 'if' statement expects an expression. The potential problem with your code above is that it evaluates … | |
Re: It means [code=python] exec mycode in mydictionary [/code] | |
Re: Use the builtin function bytearray() [code=python] >>> L = bytearray(10) >>> L bytearray(b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00') >>> L[3] = 'a' >>> L bytearray(b'\x00\x00\x00a\x00\x00\x00\x00\x00\x00') [/code] edit: sorry, that was not exacly your question, but perhaps you can use a block of 8 consecutive bytes for your purpose. | |
Re: Replace line 18 with [icode]lpre.append(subject.name)[/icode]. You can also replace line 19 with [icode]return ", ".join(lpre)[/icode]. | |
Re: I think you must specify a length for the strings, otherwise the length defaults to zero. [code=python] import numpy timefile_dtype=numpy.dtype([ ('filename', file), ('year', int), ('month', str, 25), ('day', int), ('time', str, 25), ('int_time', int), ('unit', str, 25), ('avg', int), ('boxcar', int), ]) a = numpy.zeros((1,), dtype=timefile_dtype) x = a[0] x['filename'] … | |
Re: It's a matter of personal taste. C is reasonably easy to learn, and it's a whole culture which will help you understand 50 years of programming and OS development. | |
Re: It's a design issue. Here is my son's school report [code=test] FRANCAIS Mrs xxx 17,0 , 13,0 , 16,0 , 19,0 , 10,5 , 18,0 LATIN Mrs xxx 5,0 ANGLAIS LV1 Miss xxx 12,5 , 18,0 ESPAGNOL LV1 Mrs xxx 12,5 , 8,0 , 12,0 HIST/GEO/ED.CIV Miss xxx 15,0 , … | |
Re: Perhaps use this snippet [url]http://www.daniweb.com/software-development/python/code/257449[/url] instead of os.system() | |
Re: I didn't run your code, but it seems to me that lines 45-47 remove the lines that start with "-". I suppose there was some reason why you wrote this block of code ? | |
Re: As you can see, smtplib uses another module called 'email' which is a package in the standard library. I think you should rename your program to something else than 'email.py' (also why is your script in a folder called 'dont_use' ? Perhaps you should not use it :). | |
Re: The problem is that you're passing a string 'fyzika' to add_mark() instead of a Cpredmet object. I think it would be easier if self.predmety was a dictionary [icode]name --> Cpredmet instance with that name[/icode]. For example you would write [code=python] def add_subject(self, predmet): ''' @param predmet: objekt typu predmet ''' … | |
Re: It's not a real life program, it's an academic exercise. Basic recursion is related to the mathematical notion of 'induction formula'. Here the more obvious induction formula is that [icode]x ** y = x * (x ** (y-1))[/icode]. From this idea, you should be able to write a recursive function. … | |
Re: Perhaps use a memory file (wx.MemoryFSHandler) ? | |
Re: [code=python] def gv(s): return globals()[s] print gv(x[0]) [/code] | |
Re: You must learn to read the traceback, which is [code=text] File "./jeuidiot.py", line 18 print "Could there be a key? ^ SyntaxError: EOL while scanning string literal [/code] It means that python encountered an End Of Line error at line 18 while the parser was scanning a literal string. I … | |
Re: I suppose that you are using numpy for matrix computations, aren't you ? What's the content of the matrices, real numbers ? I think it would be helpful if you post a short working example of a too long computation which you would like to speed up. | |
Re: Normally, such redirection is done with a return statement or by raising an exception. I suppose that checkdir() is called by some other function which needs an opened directory, one way to do it is to return a boolean from checkdir [code=python] def other_function(self): ... if not self.checkdir(): return # … | |
Re: There is also [icode]re.search(r'\b3\b', '13 12 333 453 2 3 433')[/icode] but it will match -3 too. | |
Re: You could first install the numpy and scipy modules which contain many special functions. To answer tour second question, you want to write a separate module containing your own special functions. For example write a mymathfuncs.py file containing [code=python] # mymathfuncs.py import numpy def arccoth(z): """Arc hyperbolic cotangent""" return numpy.arctanh(1.0/z) … | |
Re: It would help if we could see the file names. Can you run the following session with your python interpreter and post the output (with [b][code][/b] tags): [code=python] >>> import os >>> p = u"C:/path/to/folder/containing/input/files" # <-- don't forget the leading u >>> L = os.listdir(p) >>> print(L) >>> print([s.encode('utf8') … | |
Re: Why do you want your program to sleep ? You program does nothing but wait between clicks, isn't that sufficient ? If you add a sleep() somewhere, the gui will be unresponsive while the program sleeps. | |
Re: The simplest way is [code=python] import subprocess subprocess.call("./a /System/library/extensions/applehda.kext", shell=True) [/code] If you want exit status, output and error from your command, you can also try [url]http://www.daniweb.com/software-development/python/code/257449[/url] | |
Re: Use [icode]f = file.read()[/icode] and [icode]f = f.split()[/icode]. | |
Re: Why don't you want to use existing programs ? It simply means rewriting these programs in your own code. | |
Re: I would say that the time complexity of the first function is O((j-i) Log(j-i)). The rough argument is this: it is obvious that the number of 'print' only depends on j-i. Call f(z) the number of items printed when prel(x, i, j) is called with an interval length z = … | |
I discovered and installed a firefox addon called Remote Control. This addon starts a telnet server which lets you send javascript commands to the firefox web browser. Combined with python telnetlib module, it becomes very easy to reload or change the content of a tab in a running firefox from … | |
Re: There are python modules to parse vcards. I pip-installed a module called [b]vobject[/b] and it works: [code=python] #!/usr/bin/env python # -*-coding: utf8-*- import vobject cards = ["""BEGIN:VCARD VERSION:2.1 REV:20110913T095232Z UID:aac119d5fe3bc9dc-00e17913379d6cc8-3 N;X-EPOCCNTMODELLABEL1=First name:;Maj;;; TEL;VOICE:09120000000 X-CLASS:private END:VCARD""", """BEGIN:VCARD VERSION:2.1 REV:20110228T083215Z UID:aac119d5fe3bc9dc-00e17b0693898c98-4 N;X-EPOCCNTMODELLABEL1=First name:;Ali jahan;;; TEL;VOICE:09120000001 X-CLASS:private END:VCARD""", """BEGIN:VCARD VERSION:2.1 REV:20110228T083510Z UID:aac119d5fe3bc9dc-00e17b069df653a0-5 N;X-EPOCCNTMODELLABEL0=Last … | |
![]() | Re: Here is a possible solution without regexes [code=python] def add_dash(str1, str2): src = iter(str2) return "".join('-' if x == '-' else next(src) for x in str1) s1 = "75465-54224-4" s2 = "245366556346" print add_dash(s1, s2) """my output --> 24536-65563-4 """ [/code] Notice that the 6 at the end of s2 … |
Re: If you're with python 2, try ssh with paramiko. See this thread for examples [url]http://www.daniweb.com/software-development/python/threads/405462[/url] | |
Re: I suppose that you normally access your own website using an ftp client, so you should be able to list remote directories with the ftplib module. | |
These two functions compute the orders of the lowest bit and the highest bit set in the binary representation of an integer. I expect them to handle securely very large integer values. | |
Re: Did you try [code=python] next(os.walk(directory)) [/code] In the doc, it seems that os.walk only works with strings (unicode), so that you shouldn't have encoding/decoding issues. | |
Re: The program has no way to know it was launched with a subprocess.call() statement. What you could do is try to launch the program with a command line in a terminal (or cmd window) to see if it works. A command that works in shell should also work in subprocess.call() … | |
![]() | Re: No it doesn't work: [code=python] >>> a = pack('<I', 3) >>> b = pack('<I', 257) >>> a '\x03\x00\x00\x00' >>> b '\x01\x01\x00\x00' >>> a < b False [/code] pack() returns strings, which are compared in lexicographical order. == tests should work. |
Re: [QUOTE=pwolf;1737303]whilst this thread is open, could you help me with another problem? this time i am to determine whether its scalene or not, so i wrote; [CODE] def isScalene(x, y, z): if x <= 0 or y <=0 or z <=0: return False elif x == y or x == … | |
Re: I guess it could be something like this in C [code=C] #include <math.h> double dot(int sz, double *u, double* v){ int i; double res = 0.0; for(i = 0; i < sz; ++i) res += u[i] * v[i]; return res; } void cumul(int sz, double *u, double* res){ int i; … | |
This snippet defines a decorator @mixedmethod similar to @classmethod, which allows the method to access the calling instance when it exists. The decorated functions have two implicit arguments [i]self, cls[/i], the former having a None value when there is no instance in the call. Using python's descriptor protocol, the implementation … | |
Re: [url=http://lmgtfy.com/?q=python+regex+tutorial]Here is the solution.[/url] | |
Re: There are problems sometimes with the interpretation of end of line characters, depending on your platform and the program which opens your file. If your output file was written using opening mode 'wb', try mode 'w', it may solve the issue. | |
Re: I don't know anything of PyQt, but usually GUI toolkits have special ways of handling threads. Also even in non gui programs, python threads are usually started with the threading module instead of the lower level thread module. Reading this tutorial [url]http://diotavelli.net/PyQtWiki/Threading,_Signals_and_Slots[/url], it seems that subclassing QThread would be a … |
The End.