Referencing Model with String Input

Referencing Model with String input

Try:

klass = params[:type]
klass.singularize.classify.constantize.find(params[:id])

Edit:

@object = params[:type].singularize.classify.constantize.find(params[:id])

How do I reference to a class using a string in python?

I figured it out eventually, and I did it by using dictionaries. Here is the code behind it.

def Main():
class aClass():
name=""
myClass=aClass()
myClass.name="this"

classes={}
classes["myClass"]=myClass

userClass=str(input('class name'))
print(classes[userClass].name)

Main()

Method for translating a user input string to reference a pre-existing class object?

Use a dict:

entities = {
'imperialGuard': Entity('Imperial Guardsman', 10, '3/6/9/18', 'Lasgun', 'Knife')
'orkBoy': Entity('Ork Boy', 12, '3/6/9/18', 'Slugga', 'Choppa')
}

Then you can use the user input to look up the appropriate object. For example,

entities[entityName].apply_damage()

How would I reference something akin to an input more than once with list comp in Python?

One approach is to store it inside its own list and unpack it using for

string = input("String: ")

would become

for string in [input("String: ")]

>>> print([letter for string in [input("String: ")] for letter in string if letter == string[(len(string) - 1) // 2]])
String: abcde
['c']

formatted over multiple lines:

>>> print(
... [letter for string in [input("String: ")]
... for letter in string
... if letter == string[(len(string) - 1) // 2]]
... )

Also, your logic may have undesired behaviour.

String: abcdecccccc
['c', 'c', 'c', 'c', 'c', 'c', 'c']

Save user form input to model and display in view (Object reference not set to an instance of an object)

Edit
view:

@model Test.Models.UserInfo
@{
ViewBag.Title = "Hello";
Layout = "~/Views/Shared/_Layout.cshtml";
}

@using (Html.BeginForm("Hello", "Test"))
{
@Html.LabelFor(m => m.Name)
@Html.TextBoxFor(m => m.Name)

@Html.LabelFor(m => m.Age)
@Html.TextBoxFor(m => m.Age)

@Html.LabelFor(m => m.Location)
@Html.TextBoxFor(m => m.Location)

<input type="submit" value="Send" />
}

@ViewBag.Message
<br />
@Html.DisplayName(Model.DisplayInfo)

controller:

using System;
using System.Web.Mvc;
using Test.Models;

namespace Test.Controllers
{
public class TestController : Controller
{
public ActionResult Hello(UserInfo userInfo)
{
if (!String.IsNullOrEmpty(userInfo.Name))
ViewBag.Message = "Success";
return View(userInfo);
}
}
}

model:

using System;

namespace Test.Models
{
public class UserInfo
{
public String Name { get; set; }
public String Age { get; set; }
public String Location { get; set; }

public String DisplayInfo => "Name: " + Name + " Age: " + Age + " Location: " + Location;
}
}

RNN: Get prediction from a text input after the model is trained

Based on the ValueError and prediction = b.predict(np.array(stringy)), I think you need to tokenize your input string.



Related Topics



Leave a reply



Submit