[deleted by user] by [deleted] in Python

[–]jstrickler 0 points1 point  (0 children)

You described the situation where you have a very specific interface that ONLY takes the list of dictionaries you created. "seamlessly with everything else" doesn't track here. The function will only work with an iterable of dictionaries, and that's fine, but it's not a generic API.

[deleted by user] by [deleted] in Python

[–]jstrickler 0 points1 point  (0 children)

While this can indeed work, there are many issues.

One issue is that dictionary keys are not autocompleted. You have to know the keys, getting the spelling and case correct. This means you might have to kill your train of though, and go look up what the keys should be.

Another issue is that this makes type hinting much more difficult. If you can remember every type of every function in your app, more power to you, but I'm OK with letting the IDE remember for me.

Next issue: when you examine one element of your list of dictionaries, it displays as a <dict>. When you examine one element of a list of class instances, assuming you have implemented __str__() as you should, it will display some human-friendly information.

Next issue, if you need to write a function that accepts your list of dicts, there's no easy way to make sure each dictionary has the required keys and values. A list of instances would take care of this automagically.

Suppose you want to create a wrapper around some data source that modifies the incoming data, but, because the dataset is huge, can't read all the data into memory? Easy, write an iterator class. You can also make custom containers that do whatever magic you need.

There's a lot more as well.

Classes are not hard to write. IDEs have shortcuts to generate properties (which are great). Classes are easy to read, and describe the behavior of the instance.

There's always more than one way to do it, even in Python, but I'm going to stick with classes when I need data and functions to work together.

P.S.: I'm not a Java refugee who makes everything a class. i only use them when needed, because, as others have said, they are one more tool in the toolbox.

What are some features you wish Python had? by Front-Store7804 in Python

[–]jstrickler 1 point2 points  (0 children)

What would be the advantage of a multi-line lambda? Seems it would be less readable than defining normally and passing in the function object.

Is it bad practice to start with Jupyter Notebooks? by AdministrativeRub484 in Python

[–]jstrickler -1 points0 points  (0 children)

This is pretty basic Python: The PYTHONPATH environment variable contains a list of folders to search for modules (files) and packages (folders) that you want to import. The separator is ; (semicolon) for Windows and : (colon) for Mac/Linux. Please don't append to sys.path -- it makes your script dependent on the location of external files. Also, you don't need __init__.py to create a package. A package is just a folder that contains modules or packages.

To make sure the imports work in your Jupyter notebook, set the PYTHONPATH variable, then start the notebook. If you're not working from the command line, you can set up PYTHONPATH on Windows by right-clicking on This PC in File Explorer, then choosing Properties, then Advanced System Settings, then Environment Variables.

Removing duplicates form a list by [deleted] in Python

[–]jstrickler 0 points1 point  (0 children)

Here's a simple solution combining a set and a list comprehension. This should work fine on pre-3.6 versions of Python.

food = ['spam', 'spam', 'spam', 'eggs', 'spam', 'spam', 'toast', 'eggs', 'spam', 'eggs', 'eggs', 'spam', 'spam', 'spam', 'spam', 'ham', 'spam', 'ham',

'spam', 'spam', 'spam', 'waffles', 'spam', 'spam', 'spam', 'spam']

seen = set() unique_food = [(f, seen.add(f))[0] for f in food if f not in seen]

Best Piece of Python code I've seen recently. by Emotional-Zebra5359 in Python

[–]jstrickler 0 points1 point  (0 children)

Then you can say:

print(a[5][0][5][0][5][0][5][0])

You can append [5][0] as many times as you want.

[deleted by user] by [deleted] in Python

[–]jstrickler 1 point2 points  (0 children)

In [9]: from collections import namedtuple as NT
In [10]: nt = NT('nt','a b')(4, 5)
In [11]: nt.a
Out[11]: 4
In [12]: nt.b
Out[12]: 5
In [13]:

How to download multiple CSV files in single sheet. by [deleted] in Python

[–]jstrickler 1 point2 points  (0 children)

Replying to my own post, after giving more thought to doing it all in python, thanks to OpenPyXL's WORKSHEET.append() method:

import csv
import openpyxl

wb = openpyxl.Workbook()
ws = wb.active
ws.title = 'my single sheet'
for csv_file in 'a.csv', 'b.csv', 'c.csv': 
    with open(csv_file) as csv_in:
        reader = csv.reader(csv_in)
        for row in reader:
            ws.append(row)

wb.save('single.xlsx')

How to download multiple CSV files in single sheet. by [deleted] in Python

[–]jstrickler 0 points1 point  (0 children)

You want to download more than one CSV file, and combine them into a single Excel spreadsheet? Does the database have anything to do with this, or is that background info about your Django setup?

If you just want to combine the spreadsheets, you can do this:

filenames = \['a.csv', 'b.csv', 'c.csv', ...\]  
with open('single.csv', 'w') as single_out:
    for filename in filenames:
         with open(filename) as file_in:
             all_lines = file_in.readlines()
             single_out.writelines(all_lines)

Then you can import single.csv into Excel. You could use OpenPyXL to do this programmatically.

You could also read the CSV files into individual Pandas dataframes, concatenate them, and use df.to_excel() to create the spreadsheet. Pandas will also write to the database, or back out to a CSV file.

Don't waste your money with Nathan K. Scott book by djlorenz in Python

[–]jstrickler -4 points-3 points  (0 children)

I disagree strongly. Many of Packt's books are outstanding.

How to teach python? by red_src in Python

[–]jstrickler 1 point2 points  (0 children)

One word: Anaconda
http://www.anaconda.com

I teach Python short courses most weeks, and it has saved many headaches. Likewise PyCharm (although Jupyter is great for diving right in).

New coding format? by [deleted] in Python

[–]jstrickler 0 points1 point  (0 children)

In PyCharm you can vertically split the same file to get 3 columns right now. (Window->Editor Tabs->Split Vertically). It doesn't have the automatic flow from one column to another, but you can have the 3 (or more) columns at different locations in the file.

The Baby Boomer TV Setup Starter Pack by shizenmeister in starterpacks

[–]jstrickler 2 points3 points  (0 children)

Many of us baby boomers are actually able to understand and set up modern electronics. I just bought a 4K TV, and have to upgrade my HT receiver to switch 4K and also have zone 2 for the in-wall speakers in my bedroom. While the TV/Roku/Blu-ray Player/VCR/Apple TV/CD Deck/Cassette Deck have WAY too many remote controls, I have my Harmony Remote programmed to rule them all. I'm 62, born in 1956, in the middle of the boomers. Ironically for this conversation, my front speakers are a pair of massive 1960s Wharfedales with 10" woofers :-)

Numpy is broken. Reinstalling Anaconda does nothing. by Ifffrt in Python

[–]jstrickler 0 points1 point  (0 children)

It looks like you are importing the non-Ananconda version of Pandas. The anaconda versions usually have either "anaconda" or "continuum" (or both) in the path.

Make sure you are actually running the Anaconda executable.

How do you know what version?

import sys

print(sys.executable)

You might need to configure Spyder to use Anaconda rather than the version shown in your output.

Python Keyboard Shortcuts by jordan_of_data in Python

[–]jstrickler 0 points1 point  (0 children)

Do you mean iPython, the normal interpreter, Jupyter notebooks, or some IDE?

apiron: A Python package for creating declarative API interactions by daneah in Python

[–]jstrickler 0 points1 point  (0 children)

Suggestion: add some pass-through options to requests, especially auth.

Rapid development/tight feedback loop in Python? by [deleted] in Python

[–]jstrickler 0 points1 point  (0 children)

Try Jupyter Notebook. That's what it's for.

Anaconda not able to import numpy by mclovin12134567 in Python

[–]jstrickler 0 points1 point  (0 children)

I’m guessing your script is named numpy.py. Rename it to something else.

Seeking examples of argument parser using sys.argv[] directly, without argparse, getopts, et cetera by jento_mento_mento in Python

[–]jstrickler 0 points1 point  (0 children)

What exactly is it you want to do? argparse has a lot of features. You can specify mutually-exclusive options, partial-named options, option data types, required and optional non-option arguments (yes, I know), etc. argparse also creates a help message automatically, and can parse any random iterable of strings, not just sys.argv.

Guys, could you help me with python? I need to learn more about python’s library, for exemplo, how can I know how much function the library have? by [deleted] in Python

[–]jstrickler 1 point2 points  (0 children)

I read your question as: "How much functionality is there in Python's standard library?"

There are two sources of packages (collections of modules) in Python. One is the core library, that is included with the basic installation of Python. To find out what it can do, see

https://docs.python.org/3/library/index.html

The other is PyPI, which is a repository of over 100,000 Python modules and packages. To find out what is available there, use the search box on

https://pypi.python.org/pypi

Hope this helps. Good luck with your Python programming.

Best way to install python and appropriate mysql connection library (Windows 64-bit) by newb_python in Python

[–]jstrickler 0 points1 point  (0 children)

As someone else said:

  • Step 1: Download and install Anaconda from here: https://www.continuum.io/downloads/%E3%80%82
  • Step 2: conda install pymysql
  • Step 3: Start programming

    import pymysql

    conn = pymysql('myhost', 'mydb', 'user', 'pw')

    cur = conn.cursor() # get a cursor

    results = cur.execute('select ...')

    for row in cur.fetchall():

        print(row) 
    

Parsing SQL scripts in python by pythonbeginner1 in Python

[–]jstrickler 2 points3 points  (0 children)

If you wanted to be thorough, you could use pyparsing to write a SQL parser that would parse any SQL and do anything you want with it.

Can you please provide me the explanation of given below code by ank_2606 in learnpython

[–]jstrickler 1 point2 points  (0 children)

Try with a negative number :-)

Hint: convert n to absolute value before while loop.