Hot answers tagged

9

Your imports and module structure seems like an incredible mess. Yet your application only contains 3 files. Let's try and improve that. Value your namespaces import this Preferably in an interactive session, will give you an insight of how you should approach problems in Python. In your case, it is the last line Namespaces are one honking great idea -- let'...


9

I'll add my small contribution even though I'm not really a Python developer (hopefully some day). I've taken your update method and tested it on my computer (desktop, i3-4360@ 3.70GHz × 4 ), without the SDR bit. And my intuition was right! Like in most languages allowing you to do it, don't ever do that: self.fft_data = np.append(self.fft_data, self....


8

Your TalkDetailsWidget is underdeveloped, I think. You could say that you have a view but no model, and that is causing you problems. You want to be able to write talk = self.talkDetailsWidget presentation = Presentation(title=talk.title, speaker=talk.presenter, # ← Why the inconsistent vocabulary? ...


7

The biggest problem with this code is that I have no idea what it does. Complicated code is not inherently bad – sometimes we’re just solving thorny problems, and we need an in-depth solution. But it’s very hard for me to work out what this code is doing, and how it’s supposed to work. The only comments in the program are just telling me what the code ...


6

If you want enum types but can't upgrade to Python 3.4, consider using the backport of its standard library enum module. The use of the class method and class attributes in TreeData seems to me a bit like two classes crammed into one. Consider moving the class level stuff to another class, maybe like this: class TreeDatas(object): def __init__(self): ...


5

I think @JanneKarila already raised the most important points, especially critical of those is the global progress variable. I can only add some minor points on top of that. In any case, this is pretty nice code, well done! I suggest to put it up on GitHub so it's easier to clone and play with. Progress.set_option This method manipulates an activity: it ...


5

Problems For Python 2 and 3 compatibility xrange would need to be defined in both versions, e.g.: try: xrange except NameError: xrange = range Also, in Python 3 I get a lot of errors because mod isn't set: Traceback (most recent call last): File "qt.py", line 64, in keyPressEvent self.modEvent.emit([x*self.bs/2 for x in mod]) ...


5

From your answer to my comment, I will drop the intended Python 3 paragraph (even though there are some goodies you may like in the most recent versions, such as the new pathlib that supersedes the old os.path module) and go straight to the main point: Qt Designer is the way to go Writing user interface code is something that nobody likes to do. You could ...


5

I would like to point out some minor things about your style. Following PEP8 style guide makes code easier to read :) Class names should normally use the CapWords convention. Imports should be grouped in the following order: standard library imports related third party imports local application/library specific imports So your imports would be: from sys ...


5

You have a pile of unstructured, global code; this needs to be organized into functions and maybe classes Close your cursor once you're done with it, ideally via context management Order your query using the database itself, to make grouping less painful; also query in order of hierarchy (artist, album, song) You don't need entries; just add items one at a ...


4

PEP394 First #!bin/usr/python should be #!/bin/usr/python2. This is as you should state the version of python that it works on. python = Python2 and Python3 python2 = Python2 python3 = Python3 This is as Arch Linux changed python to python3, not python2. PEP8 Two spaces between module level functions and classes. One space between methods. Classes are ...


4

setup_toolbar from settings.py Maintaining a several lists in parallel is tedious. setup_toolbar has a minimal check with assert len(names) == len(icons) == len(methods) == len(tips) == len(key_shorts) to help here, but even if all the lists have the same length, there is no way to make sure that those values are really consistent. I'd propose to use ...


3

List of major edits in PainterField module: 1) Removed useless import from beginning (main module doesn’t directly use custom classes). 2) Iteration over dictionary elements: Had a lot of (see def draw_field()): for xy in squares.keys(): square = dict[xy] do_smth Fixed with: for square in dict.squares(): do_smth 3) Used dictionary mapping ...


3

This complete script works for me with PyQt on Windows, displays a small window and waits for me to close it. from PyQt4 import QtGui app = QtGui.QApplication([]) scene = QtGui.QGraphicsScene() scene.addText("Hello, world!") view = QtGui.QGraphicsView(scene) view.show() app.exec_() A QApplication object must be created before any other GUI objects.


3

While there is no hard and fast rule, it seems to make code more readable and modular (i.e., Pythonic) to have a separate class for something that is effectively a new Qt widget. This is especially true if there is any chance that new widget might be something you want to break out and use in another project. See the example labelledwidgets.py from Chapter ...


3

I consider Code-Review as 'just before releasing or deploying the code', huge commented out blocks such as: #RtlSdr set up ##sdr = RtlSdr() ## ## ##num_samples = 4096 ## ##freq_range_lo = 50e6 #experimental center freq for now ##freq_range_hi = 600e6 #1750e6 ##freq_step = 1e6 ##sample_rate = 2e6 ## ###Set initial parameters for RTL-SDR Dongle ##sdr....


3

I noticed your commenting style and decided it's worthy of it's own answer. You admit the comma is not always a comma. So don't call it a comma. I went for separator, which is as neutral as it gets. If you think of something better, go for it. Also, I think your comments would fit better as a docstring than as comments. class DecimalValidator(...


3

I would change your setup code to take a list of lists, where the inner lists are lists of subjects on that day and the outer list collects all these lists: schedule = [['Spanish', 'Spanish', 'Music', 'Music', 'English Literature', 'English Literature', 'History', 'History'], ...] def format_day(day_schedule): return "\n" + "\n".join("{} ...


3

You shouldn't use paths C:\Users\SalahGfx\Desktop\Password Generator Files\ - other user can put program in different folder and it will not work (even on other Windows). You should use sys.argv[0] to get correct folder on any computer. And then you can use os.path.join(folder, "some_file.png") to create path to images. #!/usr/bin/env python3 should be ...


3

Very nice, there are no many things to improve. I guess that this part of your code WORDS = ['Captivity', 'America', 'Europe', 'Federal', 'Gluten', 'Ridiculous', 'Automatic', 'Television', 'Difficult', 'Severe', 'Interesting', 'Indonesia', 'Industrial', 'Automotive', 'President', 'Terrestrial', 'Academic', 'Comedic', 'Comical', 'Genuine', 'Suitcase', '...


3

Nice code, so only some comments. ONE_DIE_PARMS = 1427, 30, 162, 201 TWO_DICE_PARMS = 1265, 30, 324, 201 Never used, typo in the first of them. In contrast, there are other magic numbers in your code which deserve names for them. DICE = ('dice_1.png', 'dice_2.png', 'dice_3.png', 'dice_4.png', 'dice_5', 'dice_6.png') may become DICE_TEMPLATE = '...


3

I think there is a mistake, this line is defined twice (line 28): self.check_box2.stateChanged.connect(self.onStateChange) Therefore the event onStateChange is triggered twice when you check check_box2, which is logical. I think you did a copy paste and the last should be check_box3. But your naming conventions are not intuitive, give your objects some ...


2

For various reasonnings I’ll need slightly different goods_data than yours. As such, I used the following ones which includes 2 different years with duplicate months: goods_data = { "2015" : { "09" : { "01" : { "01" : {"name" : "Red apples", "price" : "10.01", "category" : "Food and Grocery | Fruit and Vegetables | ...


2

This code compares the size of the lists twice: x = lst2 if len(lst2) > len(lst1) else lst1 y = lst1 if len(lst1) < len(lst2) else lst2 It would be better to do that only once: longer, shorter = (lst2, lst1) if len(lst2) > len(lst1) else (lst1, lst2) Notice that I also used better names instead of the meaningless x, y. With this change the ...


2

self.fileToolbar = self.addToolBar("File actions") self.fileToolbar.addAction(self.fileNewAction) self.fileToolbar.addAction(self.fileOpenAction) self.fileToolbar.addAction(self.fileSaveAction) self.fileToolbar.addAction(self.fileSaveAsAction) self.itemToolbar = self.addToolBar("Item actions") self.itemToolbar.addAction(self.undoAction) ...


2

Avoid wildcard imports Don't use wildcard imports like this: from PyQt4.QtCore import * from PyQt4.QtGui import * Quoting from PEP8: Wildcard imports (from import *) should be avoided, as they make it unclear which names are present in the namespace, confusing both readers and many automated tools. I guess you do this because you're using many ...


2

I quite that you've chosen to use a dictionary here as it is a readable way to group your widgets with their initialisation arguments, but I see two problems: You are labelling each widget twice: once in self and once in the dictionary. Your arguments are begging to be unpacked with * I would tackle both of these by grouping the name, the initialisation ...


2

Abstract Model There are a few improvements I could see in your table model class: In data() you could use elif for all of the if-statements past the first. This will save the need to evaluate each if-condition after the first condition that was found to be True Use key in dict notation. You have this line in your data() method: if self.pixmap_cache....


2

When there are no images to show, you print a message to console and exit without showing the GUI. The problem is that a GUI user probably won't see the console and is left wondering why the program won't start. You could show your window with the text in a label, instead. The list of images gets loaded in an indirect way when nextImage sees the list is ...


Only top voted, non community-wiki answers of a minimum length are eligible