1

I'm looking for a solution to limit the number of characters a user can type in a raw_input. I have two raw_inputs in a same line, that looks like this:

Name: (user input)                           Date of Birth: (user input)

If a user types in a name that is bigger than X amount of characters, it will start writing on top of Date of Birth, like this:

Name: Mary Jane Smith McDonald Obama Romney Bushh of Birth: (user input)

The closest thing I found was this: Limiting Python input strings to certain characters and lengths, but that wouldn't work for me. I don't want it to show a warning; I want it to simply stop accepting more characters and/or go to the next input.

I'm using blessed, which is a library that uses curses to position the second input in the same line as the first.

Any ideas?

Community
  • 1
  • 1
John Lisboa
  • 73
  • 1
  • 2
  • 12
  • 1
    Unfortunatley, raw_input() is not going to work for you. What you'll need to do is manually handle keyboard input and perform the rendering yourself through curses (or blessed). – Aldehir Dec 26 '15 at 02:23
  • @Aldehir I see. And do you have any idea how could I do that using blessed? – John Lisboa Dec 26 '15 at 02:46
  • FWIW, enforcing a length limit on a person's name is probably not a great design strategy. – PM 2Ring Dec 26 '15 at 02:49
  • @PM2Ring that is true! But I did leave enough space for a pretty big name... – John Lisboa Dec 26 '15 at 03:01

2 Answers2

1

The function getstr() which is the raw_input alternative for curses, works with a character constraint:

s = stdscr.getstr(0,0, 15) #Input is limited to 15 characters, once the limit is hit, it stops accepting more
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
  • That worked like a charm! My only issue is that I'm already using 'blessed' for a lot of things that would be otherwise overly complicated with 'curses', and I can only use one at a time. Do you know if blessed, or any other curses based library for that matter, has that function? – John Lisboa Dec 26 '15 at 02:48
1

Here is a neat thing I just whipped up, might help someone viewing this thread that isn't using curses. Effectively constrains string length for user input during the input.

import readchar
import sys

string = ""

while len(string) < 20:
    c = readchar.readchar()
    sys.stdout.write(c)
    string += c

print '\n' + string
vesche
  • 1,842
  • 1
  • 13
  • 19
  • That is exactly what I was looking for, but when I tried to implement that to my code I got the following error: "sqlite3.ProgrammingError: You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings." – John Lisboa Dec 26 '15 at 03:15
  • Would probably need to look at your full source code to give you the best answer. It sounds like you're saving the user inputs into a sqlite database with something like `cursor.execute('INSERT INTO table VALUES (1,1,1,1)', user_input)`. You should force the user_input to be unicode, `unicode(user_input)`. – vesche Dec 26 '15 at 03:25
  • That is exactly what I got but I can still send you the code if you want me to. The problem is, I'm using UTF-8 characters, so I can't use unicode. I first tried to simply replace "(string)" with "(name)" which is a raw_input, but that returned no results. Then I tried "name = "Name:" and that's when I got the error. – John Lisboa Dec 26 '15 at 03:52
  • Try `user_input.decode('utf-8')`? If not, post a gist or pastie to your code and I'll take a look. – vesche Dec 26 '15 at 04:03
  • this is the original code: https://gist.github.com/lovemac15/c5e71e0b8aa428693e5b and this is after I implemented the code you suggested: https://gist.github.com/lovemac15/b24702f5d21f5eb67b02 this is not the application I was referring to in the question, since I haven't built it yet. I'm trying to limit the raw_input "matricula". – John Lisboa Dec 26 '15 at 13:19