473,795 Members | 2,830 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Convert string to command..

I want to convert a string to command..
For example i have a string:
a="['1']"
I want to do this list..
How can i do ?

Oct 18 '07
35 4929
On Oct 18, 6:35 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
Abandoned wrote:
On Oct 18, 6:14 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
Abandoned wrote:
Thanks you all answer..
But "eval" is very slow at very big dictionary {2:3,4:5,6:19.. ..}
(100.000 elements)
Is there any easy alternative ?
How big? How slow? For me, a 10000-element list takes 0.04 seconds to be
parsed. Which I find fast.
Diez
173.000 dict elements and it tooks 2.2 seconds this very big time for
my project

Where does the data come from?

Diez
Data come from database..
I want to cache to speed up my system and i save the dictionary to
database for speed up but eval is very slow for do this.
Not: 2.2 second only eval operation.

Oct 18 '07 #11
On Thu, 18 Oct 2007 08:41:30 -0700, Abandoned wrote:
import cPickle as pickle
a="{2:3,4:6,2:7 }"
s=pickle.dumps( a, -1)
g=pickle.loads( s);
print g
'{2:3,4:6,2:7}'

Thank you very much for your answer but result is a string ??
In Python terms yes, strings in Python can contain any byte value. If you
want to put this into a database you need a BLOB column or encode it as
base64 or something similar more ASCII safe.

Ciao,
Marc 'BlackJack' Rintsch
Oct 18 '07 #12
Abandoned wrote:
On Oct 18, 6:35 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
>Abandoned wrote:
On Oct 18, 6:14 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
Abandoned wrote:
Thanks you all answer..
But "eval" is very slow at very big dictionary {2:3,4:5,6:19.. ..}
(100.000 elements)
Is there any easy alternative ?
>How big? How slow? For me, a 10000-element list takes 0.04 seconds to
be parsed. Which I find fast.
>Diez
173.000 dict elements and it tooks 2.2 seconds this very big time for
my project

Where does the data come from?

Diez

Data come from database..
I want to cache to speed up my system and i save the dictionary to
database for speed up but eval is very slow for do this.
Not: 2.2 second only eval operation.
Does the dictionary change often?

And you should store a pickle to the database then. Besides, making a
database-query of that size (after all, we're talking a few megs here) will
take a while as well - so are you SURE the 2.2 seconds are a problem? Or is
it just that you think they are?

Diez
Oct 18 '07 #13
On Oct 18, 6:57 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
Abandoned wrote:
On Oct 18, 6:35 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
Abandoned wrote:
On Oct 18, 6:14 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
Abandoned wrote:
Thanks you all answer..
But "eval" is very slow at very big dictionary {2:3,4:5,6:19.. ..}
(100.000 elements)
Is there any easy alternative ?
How big? How slow? For me, a 10000-element list takes 0.04 seconds to
be parsed. Which I find fast.
Diez
173.000 dict elements and it tooks 2.2 seconds this very big time for
my project
Where does the data come from?
Diez
Data come from database..
I want to cache to speed up my system and i save the dictionary to
database for speed up but eval is very slow for do this.
Not: 2.2 second only eval operation.

Does the dictionary change often?

And you should store a pickle to the database then. Besides, making a
database-query of that size (after all, we're talking a few megs here) will
take a while as well - so are you SURE the 2.2 seconds are a problem? Or is
it just that you think they are?

Diez- Hide quoted text -

- Show quoted text -
I'm very confused :(
I try to explain main problem...
I have a table like this:
id-1 | id-2 | value
23 24 34
56 68 66
56 98 32455
55 62 655
56 28 123
..... ( 3 millions elements)

I select where id=56 and 100.000 rows are selecting but this took 2
second. (very big for my project)
I try cache to speed up this select operation..
And create a cache table:
id-1 | all
56 {68:66, 98:32455, 62:655}

When i select where id 56 i select 1 row and its took 0.09 second but
i must convert text to dictionary..

Have you got any idea what can i do this conver operation ?
or
Have you got any idea what can i do cache for this table ?

Oct 18 '07 #14
Abandoned <be*****@gmail. comwrites:
import cPickle as pickle
a="{2:3,4:6,2:7 }"
s=pickle.dumps( a, -1)
g=pickle.loads( s);
print g
'{2:3,4:6,2:7}'

Thank you very much for your answer but result is a string ??
Because you gave it a string. If you give it a dict, you'll get a
dict:
>>import cPickle as pickle
a = {1:2, 3:4}
s = pickle.dumps(a, -1)
g = pickle.loads(s)
g
{1: 2, 3: 4}

If your existing database already has data in the "{...}" format, then
eval it only the first time. Then you'll get the dict which you can
cache thruogh the use of dumps/loads.
Oct 18 '07 #15
On Oct 18, 7:02 pm, Hrvoje Niksic <hnik...@xemacs .orgwrote:
Abandoned <best...@gmail. comwrites:
import cPickle as pickle
a="{2:3,4:6,2:7 }"
s=pickle.dumps( a, -1)
g=pickle.loads( s);
print g
'{2:3,4:6,2:7}'
Thank you very much for your answer but result is a string ??

Because you gave it a string. If you give it a dict, you'll get a
dict:
>import cPickle as pickle
a = {1:2, 3:4}
s = pickle.dumps(a, -1)
g = pickle.loads(s)
g

{1: 2, 3: 4}

If your existing database already has data in the "{...}" format, then
eval it only the first time. Then you'll get the dict which you can
cache thruogh the use of dumps/loads.
Sorry i can't understand :(
Yes my database already has data in the "{..}" format and i select
this and i want to use it for dictionary..
in your example:
first data is a string
finally data is already string

I want to command like eval. (eval is not good because it is slow for
my project)

Oct 18 '07 #16
Abandoned <be*****@gmail. comwrites:
I select where id=56 and 100.000 rows are selecting but this took 2
second. (very big for my project)
I try cache to speed up this select operation..
And create a cache table:
id-1 | all
56 {68:66, 98:32455, 62:655}
If you use Python to create this cache table, then simply don't dump
it as a dictionary, but as a pickle:

id-1 | all
56 <some weird string produced by cPickle.dumps>

When you load it, convert the string to dict with cPickle.loads
instead of with eval.
Oct 18 '07 #17
On Oct 18, 9:09 am, Abandoned <best...@gmail. comwrote:
On Oct 18, 6:57 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
Abandoned wrote:
On Oct 18, 6:35 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
>Abandoned wrote:
On Oct 18, 6:14 pm, "Diez B. Roggisch" <de...@nospam.w eb.dewrote:
>Abandoned wrote:
Thanks you all answer..
But "eval" is very slow at very big dictionary {2:3,4:5,6:19.. ..}
(100.000 elements)
Is there any easy alternative ?
>How big? How slow? For me, a 10000-element list takes 0.04 seconds to
>be parsed. Which I find fast.
>Diez
173.000 dict elements and it tooks 2.2 seconds this very big time for
my project
>Where does the data come from?
>Diez
Data come from database..
I want to cache to speed up my system and i save the dictionary to
database for speed up but eval is very slow for do this.
Not: 2.2 second only eval operation.
Does the dictionary change often?
And you should store a pickle to the database then. Besides, making a
database-query of that size (after all, we're talking a few megs here) will
take a while as well - so are you SURE the 2.2 seconds are a problem? Or is
it just that you think they are?
Diez- Hide quoted text -
- Show quoted text -

I'm very confused :(
I try to explain main problem...
I have a table like this:
id-1 | id-2 | value
23 24 34
56 68 66
56 98 32455
55 62 655
56 28 123
.... ( 3 millions elements)

I select where id=56 and 100.000 rows are selecting but this took 2
second. (very big for my project)
I try cache to speed up this select operation..
And create a cache table:
id-1 | all
56 {68:66, 98:32455, 62:655}

When i select where id 56 i select 1 row and its took 0.09 second but
i must convert text to dictionary..

Have you got any idea what can i do this conver operation ?
or
Have you got any idea what can i do cache for this table ?
I think several people have given you the correct answer, but for some
reason you aren't getting it. Instead of saving the string
representation of a dictionary to the database, pickle the dictionary
(not the string representation, but the actual dictionary) and save
the pickled object as a BLOB to the database. using pickle to re-
create the dictionary should be much faster than evaluating a string
representation of it.

Matt
Oct 18 '07 #18
Abandoned <be*****@gmail. comwrites:
Sorry i can't understand :(
Yes my database already has data in the "{..}" format and i select
this and i want to use it for dictionary..
But, do you use Python to create that data? If so, simply convert it
to pickle binary format instead of "{...}". As explained in my other
post:
id-1 | all
56 {68:66, 98:32455, 62:655}
If you use Python to create this cache table, then simply don't dump
it as a dictionary, but as a pickle:

id-1 | all
56 <some weird string produced by cPickle.dumps>

When you load it, convert the string to dict with cPickle.loads
instead of with eval.
Oct 18 '07 #19
On Oct 18, 7:40 pm, Hrvoje Niksic <hnik...@xemacs .orgwrote:
Abandoned <best...@gmail. comwrites:
Sorry i can't understand :(
Yes my database already has data in the "{..}" format and i select
this and i want to use it for dictionary..

But, do you use Python to create that data? If so, simply convert it
to pickle binary format instead of "{...}". As explained in my other
post:
id-1 | all
56 {68:66, 98:32455, 62:655}

If you use Python to create this cache table, then simply don't dump
it as a dictionary, but as a pickle:

id-1 | all
56 <some weird string produced by cPickle.dumps>

When you load it, convert the string to dict with cPickle.loads
instead of with eval.
Yes i understand and this very very good ;)
But i have a problem..
a=eval(a)
a=pickle.dumps( a, -1)
cursor.execute( "INSERT INTO cache2 VALUES ('%s')" % (a))
conn.commit()

and give me error:

psycopg2.Progra mmingError: invalid byte sequence for encoding "UTF8":
0x80
HINT: This error can also happen if the byte sequence does not match
the encoding expected by the server, which is controlled by
"client_encodin g".

Oct 18 '07 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
1755
by: | last post by:
How do I convert a single character, e.g. "a" into char for use in the 'split' command? p.s. I have option strict on Tia.
9
9331
by: ka1cqd | last post by:
I have been looking all over the place for a method to take command line arguments and convert them to a string or wstring so i can process the data and then covert the resulting strings to LPCWSTRs. I have tryed several methods and none work. They include: the convert utilities A2W but that does not compile because of unknow variables declared in the convert header file. MultiByteToWideChar which seems to work once but not the second...
4
118819
by: dba_222 | last post by:
Dear Experts, Ok, I hate to ask such a seemingly dumb question, but I've already spent far too much time on this. More that I would care to admit. In Sql server, how do I simply change a character into a number?????? In Oracle, it is:
4
1895
by: Mike Collins | last post by:
This worked in the command window while in debug mode ?table.Rows.ItemArray.ToString() "f0165f94-648f-4997-b578-11d89c8b1f61" But gives the error below when I compile. Cannot implicitly convert type 'string' to 'int' and the word ColumnName is underlined.
3
13861
by: GM | last post by:
Dear all, Could you all give me some guide on how to convert my big5 string to unicode using python? I already knew that I might use cjkcodecs or python 2.4 but I still don't have idea on what exactly I should do. Please give me some sample code if you could. Thanks a lot Regards, Gary
9
3522
by: keliie | last post by:
Hello (from Access novice), I'm building a switchboard form (using a Treeview object). The treeview is populated by two tables (tblSwitchboardParent and tblSwitchboardChild). Within tblSwitchboardChild, I have a string field called ChildArgument that contains string text of VBA code (e.g., DoCmd.OpenForm "myForm"). When users click on various portions of the Treeview object I want the Tree to either expand or open the report / form.
27
5156
by: comp.lang.tcl | last post by:
My TCL proc, XML_GET_ALL_ELEMENT_ATTRS, is supposed to convert an XML file into a TCL list as follows: attr1 {val1} attr2 {val2} ... attrN {valN} This is the TCL code that does this: set contents ]; close $fileID
4
3211
by: Franky | last post by:
I have a Command Prompt window open and select all the characters and copy them to the clipboard. I then read them from the clipboard str = CType(DataO.GetData(DataFormats.OemText, False), String) and try to convert them to unicode Dim InEncoding As Encoding = Encoding.GetEncoding(437)
0
2880
by: =?Utf-8?B?cm9uZSBtYXRpYXM=?= | last post by:
I have the same task to do but everytime I tried to parse my code I get a null value returned after executing "dtMaterials.WriteXml(swMaterials);". I am using the following code: Hope you can hep me out with this. Thanks. DataTable dtMaterials = new DataTable(); StringWriter swMaterials = new StringWriter(); swMaterials = null; string strMaterials = string.Empty;
3
19873
by: mamul | last post by:
Hi please some one can help me. how to convert char * to string? i have take char *argv from command line and want to pass to a function as string object(string str) i want to first convert argv to string object of type str, then pass to function(). please help me how to convert this
0
9522
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10443
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10165
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6783
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5565
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3728
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2921
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.