2,646 Posted Topics
Re: The problem is that due to the assignment statement at line 34, `dealer_num` becomes a local variable in `nowviewing()`. It means that it is a different variable with the same name. The solution is to add the line `global dealer_num` at the *beginning* of `nowviewing()` to tell python that this … | |
Re: Hm, `os.listdir()` returns names, one probably needs filenames = [os.path.join(directory, name) for name in os.listdir(directory) if name.endswith(('.py', '.txt'))] | |
Re: I suggest the official [python tutorial](https://docs.python.org/3/tutorial/index.html) to start with. | |
This small script named `sourcecode.py` is a command line tool to open a python module's source file with the default editor. For example the line $ python sourcecode.py xml.dom.minidom opens the file minidom.py from the python distribution with the default editor. The same works with user-defined modules having a python … | |
Re: Unindent line 13 to take the `print()` call out of the `for` loop. | |
This snippet shows how to find the complex roots of a polynomial using python (tested with python 2.6). You need the scipy or numpy module. | |
Re: Add print(type(variable)) # this can be useful too print(variable.__class__) there is also import inspect print(inspect.getmro(variable.__class__)) to get the ancestor classes. | |
Re: It may help everybody to know that this code is an [example](http://pythonhosted.org/neurolab/ex_newff.html) in module neurolab source code, which illustrates the use of the Feed Forward Multilayer Perceptron. | |
This script computes the formal series expansion of a mathematical function on the command line using the python module swiginac, an interface to the CAS Ginac. Typical invocation in a linux terminal looks like $ serexp.py -n 7 "log(1+x)*(1+x+x**2)**(-1)" 1*x+(-3/2)*x**2+5/6*x**3+5/12*x**4+(-21/20)*x**5+7/15*x**6+Order(x**7) As far as I know, swiginac does not work in … | |
Re: You can define a function to do the same easily @post_process(list) def gather(xml_doc, paths): for p in paths: node = xml_doc for word in p: node = getitem(node, word) yield node a = gather(xml_doc, (('meeting', '@id'), ('meeting', '@venue'))) `post_process` is defined in [this code snippet](http://www.daniweb.com/software-development/python/code/374530/post-process-generated-values-with-a-decorator) (very useful bit). | |
A simple way to resize an image programmatically, using the PythonMagick module. This module is a python port of the [the magick++ library](http://www.imagemagick.org/Magick++/). | |
Re: If you only changed the file `.bashrc`, why don't you simply restore the backup to see if it works ? | |
Re: Think about how to evaluate the list [5, 1, 2, '+', 4, '*', '+', 3, '-'] The `evaluate_list()` function must return 14. | |
Re: A way I know is to distribute using setuptools in [development mode](https://pythonhosted.org/setuptools/setuptools.html#develop-deploy-the-project-source-in-development-mode). Use the command python setup.py develop in the development directory and you can edit the module indefinitely. | |
Re: The function `get_inverse_dict()` currently returns `None`. After you insert your code, it is supposed to return the dictionary {'a': 'z', 'c': 'x', 'b': 'y', 'e': 'v', 'd': 'w', 'g': 't', 'f': 'u', 'i': 'r', 'h': 's', 'k': 'p', 'j': 'q', 'm': 'n', 'l': 'o', 'o': 'l', 'n': 'm', 'q': 'j', … | |
| |
Re: I would suggest using a user friendly python application such as [cherrytree](http://www.giuspen.com/cherrytree/). Each recipe could go in a separate node of the tree, wich could be organised like a cookbook. The other part of it is that cherrytree keeps the node contents in a sqlite database where data can easily … | |
Re: Perhaps play with the [.next_siblings](http://www.crummy.com/software/BeautifulSoup/bs4/doc/#next-siblings-and-previous-siblings) and `.sibling` attributes of the title tag. | |
Re: If you are looking for http requests in python, the [requests](https://pypi.python.org/pypi/requests) module should provide the necessary tools. Otherwise, there is a [pycurl](http://pycurl.sourceforge.net/) ... | |
Re: This is a typical use case for generators and tools for iterables. Instead of writing to a file, use a generator import os import xml.etree.ElementTree as ET from itertools import ifilterfalse def generate_lines(): tree = ET.parse("Logfile.xml") root = tree.getroot() for element in root.iter('Path'): file_name = os.path.basename(element.text) #.exe just an example … | |
Re: Use `os.path.exists()` or `os.path.isfile()` depending on what you want to do. | |
Re: A well known snippet >>> 'real'[::-1] 'laer' >>> | |
Re: Read the doc ! [Apparently](http://docs.opencv.org/modules/highgui/doc/reading_and_writing_images_and_video.html#cv2.imread) `cv2.imread()` expects a filename. | |
Re: > but .read() just prints the file contents not its binary What do you mean exactly ? Give a binary example (you can attach a zip or an image to a post). `read()` does not print anything, it reads the binary file's content. Please explain what you're expecting and what … | |
Re: Use one of vegaseat's tkinter [examples](http://www.daniweb.com/software-development/python/code/467528/show-internet-image-with-tkinter). You can adapt the example by reading the file from your disk instead of the internet. | |
Re: Here is a long [video tutorial](https://www.youtube.com/watch?v=iyXyxvs-544) about this. | |
Re: I'm using kubuntu, but it should work in debian: make sure you have the development packages for python (python-dev and libpython-dev): sudo aptitude search python-dev # then may be sudo aptitude install python-dev sudo aptitude install libpython-dev sudo aptitude install python-setuptools sudo aptitude install python-pip Then sudo pip install pycrypto … | |
Re: Use list comprehensions and [icode]sorted[/icode] [code=python] >>> dict1={'12':['123','187','1860','4821','7000','9000'],'18':['1234','4000']} >>> L = sorted((int(k), sorted(int(x) for x in v)) for k, v in dict1.items()) >>> print L [(12, [123, 187, 1860, 4821, 7000, 9000]), (18, [1234, 4000])] >>> M = [(k, x) for k, v in L for x in v] >>> … | |
Re: Why not post the code and the exception traceback ? | |
![]() | Re: You can improve the program by using the standard module `cmd` which purpose is to write interactive command line interpreters. I wrote an enhanced class of cmd.Cmd in [this code snippet](http://www.daniweb.com/software-development/python/code/258640/enhanced-cmd-class-for-command-line-interpreters). It makes it very easy to write an interpreter: copy the class ExampleCmd at the end of the file … |
Re: A hint: >>> line = '14 18 35 47 54 - 57\n' >>> line.split() ['14', '18', '35', '47', '54', '-', '57'] | |
Re: Try to use [this command class](http://www.daniweb.com/software-development/python/code/257449/a-command-class-to-run-shell-commands) instead of `subprocess.call()` com = Command(scriptPath).run() print(com.output) print(com.error) | |
Re: Use `re.findall()` | |
![]() | Re: I see 2 solutions 1. You wait until python 3.4 is the packaged python 3 version in your linux distro (this is currently python 3.3.2+ in my kubuntu). 2. You download the source tarball and you compile it yourself, using something like 'configure' and 'make altinstall'. There may be dependencies … ![]() |
Re: Here is a very simple example using the microframework [bottle](http://bottlepy.org/docs/0.11/tutorial.html) (install with pip for example) # -*-coding: utf8-*- # webrobot.py from bottle import get, post, request from bottle import route, run @get('/movearm') def movearm_form(): return '''<form method="POST" action="/movearm"> Move the arm <input type="submit"/> </form>''' @post('/movearm') def movearm_submit(): from robotprogram import … | |
Re: Use the format method. Here are the different ways to print a floating number centered in a field of length 10, with precision of 2 digits >>> s = "{val:^10.2e}{val:^10.2E}{val:^10.2f}{val:^10.2F}{val:^10.2g}{val:^10.2G}{val:^10.2n}{val:^10.2%}" >>> print(s.format(val = 0.1)) 1.00e-01 1.00E-01 0.10 0.10 0.1 0.1 0.1 10.00% | |
Re: >>> s = set(range(10)) >>> s set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> s -= set([3,4,5]) >>> s set([0, 1, 2, 6, 7, 8, 9]) >>> s -= set(range(3,10)) >>> s set([0, 1, 2]) | |
Re: Please don't revive 6 years old threads. Also this thread is about python's tkinter. It is not at all related te asp.net or jquery. | |
![]() | Re: The answer is yes it is possible to create *global* variables this way, but it is considered _poor_ programming. Here is this forbidden fruit globals().update((name, 'foo') for name in list1) Now all the variables exist, with value the string "foo". Apart from this bad way to do it there are … |
Re: To make it short, I think you are doing this with dict `channels` D = {} for i in range(5): D[i] = "v%d" % i for D_key in D: print D[D_key], """ my output --> v0 v0 v1 v0 v1 v2 v0 v1 v2 v3 v0 v1 v2 v3 v4 … | |
Re: I suggest to read expressions and use lambda to create a python function deriv = input("Y' = ") # enter for example 2 * x func = eval("lambda x: " + deriv.strip()) # now func(3.7) --> 7.4 | |
Re: > According to > mktime takes struct_time in local time as per > "Use the following functions to convert between time representations:" section of the page As Opsive wrote 10 posts 5 years ago, we guess that (s)he is not waiting for an answer today. Please don't revive old threads. … | |
Re: Apparently it comes from tutors at python.org 7 years ago. Read [this mail](https://mail.python.org/pipermail/tutor/2007-April/053825.html). | |
My old hard drive with Linux Mint 15 Olivia refused to boot yesterday: kernel panic etc. I tried to repair the file system and now some shared libraries are missing. On this computer, I have a brand new second hard drive with Kubuntu 13.10 freshly installed. From this OS, I can … | |
Re: > will exit your program after 5 seconds This fails, but using `os._exit(0)` works, instead of `sys.exit(0)`. In my linux system, the following also works, which is very nice import thread import threading threading.Timer(2.0, lambda : thread.interrupt_main()).start() try: while True: print("stopme") except KeyboardInterrupt: print("We're interrupted: performing cleanup action.") """ my … | |
Re: You can start by writing pseudo-code to describe the different things that your program must do # print a message saying that the following table # displays temperatures in celsius and their # conversion to farenheit. Your-code-here # set the celsius temperature C to zero Your-code-here # while the celsius … | |
Re: Install an ssh server in the remote host and use module paramiko. [This post](http://www.daniweb.com/software-development/python/threads/375262/running-a-file-on-a-remote-computer-using-python#post1615004) is a little old, but it could help. | |
Re: I would try sql = 'SELECT `blob_field` FROM `Tab1`' cursor.execute(sql) for row in cursor: blob_value = row[0] | |
Re: Here is the code, reindented with tim peter's [reindent](https://pypi.python.org/pypi/Reindent/0.1.0) (not sure it would work without python 2) from tkinter import* import sys, random, tkinter ticket_price = 2 i=0 total = 0 def calculatetotals(): valid = set('12345') ticket_entry = tik.get() if ticket_entry not in valid: print('We permit values in {}.'.format(valid)) label4 … | |
Re: Apparently, the stream `sys.stdin` was closed when your program attempted to read a line. In which context did you run the program ? Which OS first ? was it in a python IDE ? was it in a terminal ? Did you type a CTRL D in a terminal while … |
The End.