Path: csiph.com!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'received:209.85.223': 0.03; 'error:': 0.05; 'paths': 0.05; '%s"': 0.07; 'host,': 0.07; 'try:': 0.07; 'python': 0.09; '(1,': 0.09; 'encode': 0.09; 'indicates': 0.09; 'information?': 0.09; 'modulo': 0.09; 'string)': 0.09; 'subject:number': 0.09; 'subject:string': 0.09; 'to:addr:comp.lang.python': 0.09; 'cc:addr:python-list': 0.10; '"error': 0.16; '%d:': 0.16; '%s,': 0.16; '(eg.': 0.16; 'alphabet': 0.16; 'created.': 0.16; 'hex': 0.16; 'hits': 0.16; 'know)': 0.16; 'losing': 0.16; 'row': 0.16; 'simple.': 0.16; 'subject: \n ': 0.16; 'subject:Converting': 0.16; 'string': 0.17; 'wrote:': 0.17; 'duplicate': 0.17; 'exists': 0.17; 'code,': 0.18; 'commands,': 0.22; 'keys': 0.22; 'please?': 0.22; 'cc:2**0': 0.23; 'insert': 0.23; 'cc:no real name:2**0': 0.24; 'external': 0.24; 'host': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; 'am,': 0.27; 'done.': 0.27; 'question': 0.27; 'record': 0.28; 'restricted': 0.29; 'strings,': 0.29; 'primary': 0.30; 'asking': 0.32; 'file': 0.32; 'etc.)': 0.32; 'could': 0.32; 'print': 0.32; 'skip:s 30': 0.33; 'received:google.com': 0.34; 'done': 0.34; 'path': 0.35; 'so,': 0.35; 'table': 0.35; 'received:209.85': 0.35; 'except': 0.36; 'but': 0.36; 'anything': 0.36; "i'll": 0.36; 'subject: (': 0.36; 'ok,': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'store': 0.38; 'page': 0.38; 'where': 0.40; 'help': 0.40; 'easy': 0.60; 'most': 0.61; 'save': 0.61; 'map': 0.61; 'kind': 0.61; 'back': 0.62; '(that': 0.62; 'more': 0.63; 'legal': 0.65; 'date,': 0.65; 'else.': 0.65; 'pin': 0.65; '8bit%:100': 0.70; '8bit%:92': 0.70; '2013': 0.84; 'back?': 0.84; 'subject:original': 0.84; '100,000': 0.91; 'obvious,': 0.91; 'urls,': 0.91; 'angel': 0.93; 'realistic': 0.93 X-Received: by 10.50.236.40 with SMTP id ur8mr510862igc.6.1358955051195; Wed, 23 Jan 2013 07:30:51 -0800 (PST) Newsgroups: comp.lang.python Date: Wed, 23 Jan 2013 07:30:49 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=94.68.70.179; posting-account=DYJQ-woAAACEPH85Au2BhUVfFTfSfVa4 References: <2c2351fb-2044-4351-af3e-63cff4fbf0f8@googlegroups.com> User-Agent: G2/1.0 X-Google-Web-Client: true X-Google-IP: 94.68.70.179 MIME-Version: 1.0 Subject: Re: Converting a number back to it's original string (that was hashed to generate that number) From: Ferrous Cranus To: comp.lang.python@googlegroups.com Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Message-ID: Lines: 104 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1358955551 news.xs4all.nl 6907 [2001:888:2000:d::a6]:60412 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:37483 =CE=A4=CE=B7 =CE=A4=CE=B5=CF=84=CE=AC=CF=81=CF=84=CE=B7, 23 =CE=99=CE=B1=CE= =BD=CE=BF=CF=85=CE=B1=CF=81=CE=AF=CE=BF=CF=85 2013 3:58:45 =CE=BC.=CE=BC. U= TC+2, =CE=BF =CF=87=CF=81=CE=AE=CF=83=CF=84=CE=B7=CF=82 Dave Angel =CE=AD= =CE=B3=CF=81=CE=B1=CF=88=CE=B5: > On 01/23/2013 08:38 AM, Ferrous Cranus wrote: >=20 > > Please DON'T tell me to save both the pin <=3D> filepath and associate = them (that can be done by SQL commands, i know) >=20 > > I will not create any kind of primary/unique keys to the database. >=20 > > I will not store the filepath into the database, just the number which = indicates the filepath(html page). >=20 > > Also no external table associating fielpaths and numbers. >=20 > > i want this to be solved only by Python Code, not database oriented. >=20 > > >=20 > > >=20 > > That is: I need to be able to map both ways, in a one to one relation,= 5-digit-integer <=3D> string >=20 > > >=20 > > int( hex ( string ) ) can encode a string to a number. Can this be deco= ded back? I gues that can also be decoded-converted back because its not lo= sing any information. Its encoding, not compressing. >=20 > > >=20 > > But it's the % modulo that breaks the forth/back association. >=20 > > >=20 > > So, the question is: >=20 > > >=20 > > HOW to map both ways, in a one to one relation, (5-digit-integer <=3D> = string) without losing any information? >=20 > > >=20 >=20 >=20 > Simple. Predefine the 100,000 legal strings, and don't let the user use= =20 >=20 > anything else. One way to do that would be to require a path string of= =20 >=20 > no more than 5 characters, and require them all to be of a restricted=20 >=20 > alphabet of 10 characters. (eg. the alphabet could be 0-9, which is=20 >=20 > obvious, or it could be ".aehilmpst" (no uppercase, no underscore, no=20 >=20 > digits, no non-ascii, etc.) >=20 >=20 >=20 > In the realistic case of file paths or URLs, it CANNOT be done. OK, its not doable. I'll stop asking for it. CHANGE of plans. i will use the database solution which is the most easy wau to do it: =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D # insert new page record in table counters or update it if already exists try: cursor.execute( '''INSERT INTO counters(page, hits) VALUES(%s, %s)=20 ON DUPLICATE KEY UPDATE hits =3D hits + 1''', (htmlpage, 1) ) except MySQLdb.Error, e: print ( "Query Error: ", sys.exc_info()[1].excepinfo()[2] ) =09 # update existing visitor record if same pin and same host found try: cursor.execute( '''UPDATE visitors SET hits =3D hits + 1, useros =3D %s, = browser =3D %s, date =3D %s WHERE pin =3D %s AND host =3D %s''', (useros, b= rowser, date, page, host)) except MySQLdb.Error, e: print ( "Error %d: %s" % (e.args[0], e.args[1]) ) =09 # insert new visitor record if above update did not affect a row if cursor.rowcount =3D=3D 0: cursor.execute( '''INSERT INTO visitors(hits, host, useros, browser, date= ) VALUES(%s, %s, %s, %s, %s)''', (1, host, useros, browser, date) ) =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D I can INSERT a row to the table "counter" I cannot UPDATE or INSERT into the table "visitors" without knowing the "pi= n" primary key number the database created. Can you help on this please?