Django CommaSeperatedIntegerField and negative numbers - django-models

If I had the following class:
class foo(models.Model):
list = models.CommaSeparatedIntegerField(max_length=255)
If I try to add a foo object from the admin, it will not let me populate list with negative numbers. For instance, if I populate the field with 1,2,3,4 its fine. But 1,2,3,-4 will give me the error message "Enter only digits separated by commas." Any ideas?
Thanks!

The validation code of CommaSeparatedIntegerField looks like this:
comma_separated_int_list_re = re.compile('^[\d,]+$')
validate_comma_separated_integer_list = RegexValidator(comma_separated_int_list_re, _(u'Enter only digits separated by commas.'), 'invalid')
So you can't use it for negative numbers.
I suggest you write your own validation code based on this code.

Related

how to write script on validating number from the text? for ex: i like to get numeric value "2" from field "Staus(2)"

On Selenium, I'm writing script to get the number from the text. suppose there is a field 'Status(2)'. The number in the brackets keep changing. I want to get the value.
This code should get back the text for the element you have provided:
WebElement web_element_found = driver.findElement(By.id("ctl00_ctl00_cphBMain_cphMain_lblObjects"));
String element_text = web_element_found.getText();
Then you can have a look at this answer for how to use regex to extract the digit from the string: Regex to extract digit from string in Java
Hope this helps!
Here is the solution.
String rawText = driver.findElement(By.id("ctl00_ctl00_cphBMain_cphMain_lblObjects")).getText();
String number = rawText.substring(s.indexOf("(") + 1).substring(0, s.indexOf(")"));
System.out.println(number);

Would like to displayonly last 4 digits on phone number using angularjs

I need to show last 4 digits phone number on page which i am going to get from API.
Can you please help me? format should be like (XXX)XXX-1234
Can you be more specific.You can use a pipe for formatting it in desired way.
While your question isn't very specific, you might consider using this: Angular
SlicePipe
That way you can get the last 4 digits of the phone number using string indices.
This is more of a javascript answer, but you could create an expression, then use it in your HTML.
$scope.formatPhone = function(phoneNumber) {
if(phoneNumber) {
return '(XXX)XXX-' + phoneNumber.substr(phoneNumber.length - 4);
}
return '';
}

Generating a reverse-engineerable code to save game, python 3

So I'm making a little text based game in Python and I decided for a save system I wanted to use the old "insert code" trick. The code needs to keep track of the players inventory (as well as other things, but the inventory is what I'm having trouble with).
So my thought process on this would be to tie each item and event in the game to a code. For example, the sword in your inventory would be stored as "123" or something unique like that.
So, for the code that would generate to save the game, imagine you have a sword and a shield in your inventory, and you were in the armory.
location(armory) = abc
sword = 123
shield = 456
When the player inputs the command to generate the code, I would expect an output something like:
abc.123.456
I think putting periods between items in the code would make it easier to distinguish one item from another when it comes to decoding the code.
Then, when the player starts the game back up and they input their code, I want that abc.123.456 to be translated back into your location being the armory and having a sword and shield in your inventory.
So there are a couple questions here:
How do I associate each inventory item with its respective code?
How do I generate the full code?
How do I decode it when the player loads back in?
I'm pretty damn new to Python and I'm really not sure how to even start going about this... Any help would be greatly appreciated, thanks!
So, if I get you correctly, you want to serialize info into a string which can't be "saved" but could be input in your program;
Using dots is not necessary, you can program your app to read your code without them.. this will save you a few caracters in lenght.
The more information your game needs to "save", the longer your code will be; I would suggest to use as short as possible strings.
Depending on the amount of locations, items, etc. you want to store in your save code: you may prefer longer or shorter options:
digits (0-9): will allow you to keep 10 names stored in 1 character each.
hexadecimal (0-9 + a-f, or 0-9 + a-F): will allow you to keep from 16 to 22 names (22 if you make your code case sensitive)
alphanum (0-9 + a-z, or 0-9 + a-Z): will allow you to keep from 36 to 62 names (62 if case sensitive)
more options are possible if you decide to use punctuation and punctuated characters, this example will not go there, you will need to cover that part yourself if you need.
For this example I'm gonna stick with digits as I'm not listing more than 10 items or locations.
You define each inventory item and each place as dictionaries, in your source code:
You can a use single line like I have done for places
places = {'armory':'0', 'home':'1', 'dungeon':'2'}
# below is the same dictionary but sorted by values for reversing.
rev_places = dict(map(reversed, places.items()))
Or for improved readability; use multiple lines:
items = {
'dagger':'0',
'sword':'1',
'shield':'2',
'helmet':'3',
'magic wand':'4'
}
#Below is the same but sorted by value for reversing.
rev_items = dict(map(reversed, items.items()))
Store numbers as strings, for easier understanding, also if you use hex or alphanum options it will be required.
Then also use dictionaries to manage in game information, below is just a sample of how you should represent your game infos that the code will produce or parse, this portion should not be in your source code, I have intentionally messed items order to test it.;
game_infos = {
'location':'armory',
'items':{
'slot1':'sword',
'slot2':'shield',
'slot3':'dagger',
'slot4':'helmet'
}
}
Then you could generate your save code with following function that reads your inventory and whereabouts like so:
def generate_code(game_infos):
''' This serializes the game information dictionary into a save
code. '''
location = places[game_infos['location']]
inventory = ''
#for every item in the inventory, add a new character to your save code.
for item in game_infos['items']:
inventory += items[game_infos['items'][item]]
return location + inventory # The string!
And the reading function, which uses the reverse dictionaries to decipher your save code.
def read_code(user_input):
''' This takes the user input and transforms it back to game data. '''
result = dict() # Let's start with an empty dictionary
# now let's make the user input more friendly to our eyes:
location = user_input[0]
items = user_input[1:]
result['location'] = rev_places[location] # just reading out from the table created earlier, we assign a new value to the dictionary location key.
result['items'] = dict() # now make another empty dictionary for the inventory.
# for each letter in the string of items, decode and assign to an inventory slot.
for pos in range(len(items)):
slot = 'slot' + str(pos)
item = rev_items[items[pos]]
result['items'][slot] = item
return result # Returns the decoded string as a new game infos file :-)
I recommend you play around with this working sample program, create a game_infos dictionary of your own with more items in inventory, add some places, etc.
You could even add some more lines/loops to your functions to manage hp or other fields your game will require.
Hope this helps and that you had not given up on this project!

AngularJS Get words with a prefix from text box

I am trying to get words with a certain prefix when a user types into a text box. For example, let's say I want all words in a text box that begins with "#". How would I go about getting these words?
One way to do it is to use the JavaScript split() method.
Let's say you get the value of a user input into one of your variables. You can use this method to get an array of String of the words that start with the prefix you chose.
var input = "#This is what the #user typed";
var splitArray = input.split("#");
// splitArray = ["This is what the", "user typed"]

How to do patchEntity in cakephp

Hi I'm making a webpage to get phone number from registerer and make random number as a pin number to save in DB.
and I want to show registerer to enter the random number.
So I made these code.
class RegistersController extends AppController{
public function index()
{
$random = rand(11111,99999);
$register = $this->Registers->newEntity($this->request->data);
if($this->Registers->save($register)) {
$this->Flash->success('The Phone number has been sent.');
$reg = $this->Registers->patchEntity($random);
$this->Registers->save($reg);
return $this->redirect(['action' => 'certnum']);
}
$this->set(compact('register'));
}
}
But somehow It makes fatal error which I have no idea what to do?
Do i have to the random number into array with certain id number or something?
Please help.
Thank you
Back to developer school: When asking for help and describing a problem always post the complete error message, any warning, notice, stack trace and other relevant debug output as well.
I guess the error message I don't know is related to the fact that you try you pass an invalid data type to patchEntity(). It expects an array, you are passing an integer, because that's what rand() generates. Put the integer in whatever array structure you expect and it should work.

Resources