Re: [Tutor] Finding the max value from a dictionary that does not exceed a variable's value.

2016-01-31 Thread srinivas devaki
On Sun, Jan 31, 2016 at 3:54 AM, Danny Yoo  wrote:
> ---
> I want to take the max value in the dictionary 'coinvalues'  that is the
> same as or lower than the variable 'change'.  I have no idea how to search
> through the 'coinvalues' dictionary and find the value that is highest but
> does not exceed the value held in the variable 'change'.
> ---

although OP's problem doesn't need this, is there a better way achieve
this other than
using a balanced binary search tree.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] event loop vs threads

2016-09-25 Thread srinivas devaki
how does Python switch execution and maintain context i.e function stack
etc,.. for co-routines and why is it less costly than switching threads
which almost do the same, and both are handled by Python Interpreter
itself(event loop for co-routines and GIL scheduling for threading), so
where does the extra overhead for threads come from ?

Regards
Srinivas Devaki
Senior (final yr) student at Indian Institute of Technology (ISM), Dhanbad
Computer Science and Engineering Department
ph: +91 9491 383 249
telegram_id: @eightnoteight
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Unicode to Ascii

2016-09-26 Thread srinivas devaki
How can I convert Unicode to Ascii by stripping of any non ascii characters.

one way is to filter on s like

ascii = ''.join(filter(lambda x: 0 <= ord(x) < 256, unicode_string))

but are there any other simple ways ?

Regards
Srinivas Devaki
Senior (final yr) student at Indian Institute of Technology (ISM), Dhanbad
Computer Science and Engineering Department
ph: +91 9491 383 249
telegram_id: @eightnoteight
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to test for the existence of a table in a sqlite3 db?

2017-10-14 Thread srinivas devaki
SQL has "CREATE TABLE IF NOT EXISTS"

so you change your instructions to
```python
import sqlite3
conn = sqlite3.connect(':memory:')
c = conn.cursor()
c.execute("""
CREATE TABLE IF NOT EXISTS BloodPressureReadings (
ReadingID INTEGER PRIMARY KEY,
Date TEXT,
Time TEXT,
SystolicBP INTEGER,
DiastolicBP INTEGER,
Comments TEXT);
""")
c.execute("SELECT * FROM BloodPressureReadings")
```


On Sat, Oct 14, 2017 at 11:13 AM boB Stepp  wrote:
>
> I want to use Alan's (and others') idea to run a SQL file to create a
> table if that table does not exist.  Alan suggested using
> executescript() to do this.  I misunderstood Alan and thought that
> this would take a filename and execute it.  Instead, it appears that I
> must pass to it a string which is a SQL script.  So after lots of
> fooling around in the interpreter I arrived at:
>
> py3: import sqlite3
> py3: conn = sqlite3.connect(':memory:')
> py3: c = conn.cursor()
> py3: try:
> ... c.execute('select * from BloodPressureReadings')
> ... except sqlite3.OperationalError:
> ... with open('create_sqlite3_db.sql') as f:
> ... sql = f.read()
> ... c.executescript(sql)
> ...
> 
>
> The file 'create_sqlite3_db.sql' contains:
>
> CREATE TABLE BloodPressureReadings (
> ReadingID INTEGER PRIMARY KEY,
> Date TEXT,
> Time TEXT,
> SystolicBP INTEGER,
> DiastolicBP INTEGER,
> Comments TEXT);
>
> So at this point I am only creating an empty table.
>
> The above "works", but my "try" check is awful!  What can I replace it
> with to just see if there is *any* table in the chosen database?  In
> the code Peter supplied in the thread, "How is database creation
> normally handled?", he used in his function, "ensure_db(filename)":
>
> cursor.execute("create table if not exists addresses (name, email);")
>
> which is sweet, but I don't see how I can apply this idea if I insist
> on using a SQL file to create my table(s).
>
> BTW, in the docs at https://docs.python.org/3/library/sqlite3.html I
> found no mention of the actual exception I caught, "OperationalError".
> Should not this be in the docs?
>
> --
> boB
> ___
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] How to test for the existence of a table in a sqlite3 db?

2017-10-14 Thread srinivas devaki
On Sat, Oct 14, 2017 at 12:41 PM, boB Stepp  wrote:
> py3: c.executescript(sql)
> 
> py3: tb_exists = "select name from sqlite_master where type='table'
> and name='BloodPressureReadings'"
> py3: tb_ck = c.execute(tb_exists).fetchone()
> py3: print(tb_ck)
> ('BloodPressureReadings',)
>
> So it is looking like I can use this technique to determine if I need
> to create the BloodPressureReadings table or not.  Am I on track here
> or is there a better technique?
>

awesome trick. but most of the time applications had to exported out
of sqlite to either mysql or postgresql at which time it would be a
lot of refactoring as this trick uses sqlite internals to check if a
table exists.

fetchone would always return a tuple as we did count in the sql
instruction. so it would be easier to embed in an if statement.

Regards
Srinivas Devaki
Software Developer at Zomato, New Delhi
Phone: +91 9491 383 249
Telegram: @eightnoteight
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor