EzDevInfo.com

humanize

python humanize functions

How to capitalize the first character of each word, or the first character of a whole string, with C#?

I could write my own algorithm to do it, but I feel there should be the equivalent to ruby's humanize in C#.

I googled it but only found ways to humanize dates.

Examples:

  • A way to turn "Lorem Lipsum Et" into "Lorem lipsum et"
  • A way to turn "Lorem lipsum et" into "Lorem Lipsum Et"

Source: (StackOverflow)

How do I set human readable attribute names in a rails model?

This seems like a really simple question -- when I'm using form_for or fields_for helpers to generate markup from my models, how can I modify my model to customize the string that appears for a particular attribute?

More or less the same question was asked before[1], but the answer was 'internationalize', and that's not what I'm trying to do, I just want to override one or two humanized attribute names.


Source: (StackOverflow)

Advertisements

Python Number to Word e.g 10K, 10.65M

I've looked into a few ways of representing numbers in a small space (in Django) and whilst their django.contrib.humanize.intword works great, i'd prefer if it said M instead of million for example. I know this wouldn't work with exceptionally large numbers (10^15 and 10^18 both start with Q).

I basically need four different unit names up to trillion: K, M, B and T. Are there any packages that allow a lot of configuration with numbers?

I've not posted any code because what I have works but not quite how i'd like it, this is just a question to see if there is a better way to get the numbers i'm hoping for.


Source: (StackOverflow)

Natural/Relative days in Python

I'd like a way to show natural times for dated items in Python. Similar to how Twitter will show a message from "a moment ago", "a few minutes ago", "two hours ago", "three days ago", etc.

Django 1.0 has a "humanize" method in django.contrib. I'm not using the Django framework, and even if I were, it's more limited than what I'd like.

Please let me (and generations of future searchers) know if there is a good working solution already. Since this is a common enough task, I imagine there must be something.


Source: (StackOverflow)

How to display "x days ago" type time using Humanize in Django template?

When I do this:

{% load humanize %}

{{ video.pub_date|naturaltime|capfirst }}

I get 2 days, 19 hours ago

How can I get just 2 days without the hours. Basically if the video was published in less than a day ago then it should say X hours ago, then it should count in days like X days ago, then in weeks. I just don't want 1 hours 5 minutes ago or 2 days 13 minutes ago. Just the first part.

I looked at the humanize docs but couldn't find what I needed.


Source: (StackOverflow)

How do I convert CamelCase into human-readable names in Java?

I'd like to write a method that converts CamelCase into a human-readable name.

Here's the test case:

public void testSplitCamelCase() {
    assertEquals("lowercase", splitCamelCase("lowercase"));
    assertEquals("Class", splitCamelCase("Class"));
    assertEquals("My Class", splitCamelCase("MyClass"));
    assertEquals("HTML", splitCamelCase("HTML"));
    assertEquals("PDF Loader", splitCamelCase("PDFLoader"));
    assertEquals("A String", splitCamelCase("AString"));
    assertEquals("Simple XML Parser", splitCamelCase("SimpleXMLParser"));
    assertEquals("GL 11 Version", splitCamelCase("GL11Version"));
}

Source: (StackOverflow)

Make big and small numbers human-readable [duplicate]

This question already has an answer here:

I would like to print my very small numbers in C# in a human friendly way, such as:

30µ for 3E-5 or 456.789n for 0.000000456789.

I know of the Humanize_number() function from BSD in C, but only compatible with bit ints, not floats and doubles. Is there the equivalent in C# that supports those?

Also, it should keep a certain amount of precision when displaying numbers, like:

0.003596 should be displayed as 3.596µ, not 3.6µ (or worse, ).

The possible answer here: Formatting Large Numbers with .NET but adapted for negative log10 is truncating the numbers to 1 digit after the comma. That's far from complete in my opinion.

Examples of how I'd like to present things:

3000        3K
3300        3.3K
3333        3.333K
30000       30k
300000      300k
3000000     3M
3000003     3.000003M // or 3M if I specify "4 digits precision"
0.253       253m
0.0253      25.3m
0.00253     2.53m
-0.253003   -253.003m

I couldn't formulate my question to find relevant answers in SO, so if the question has been already answered, fire away!


Source: (StackOverflow)

Formatting Large Numbers with .NET

I have a requirement to format large numbers like 4,316,000 as "4.3m".

How can I do this in C#?


Source: (StackOverflow)

Humanize a string in JavaScript

How do I humanize a string? Based on the following criteria:

  • Deletes leading underscores, if any.
  • Replaces underscores with spaces, if any.
  • Capitalizes the first word.

For example:

this is a test -> This is a test
foo Bar Baz    -> Foo bar baz
foo_bar        -> Foo bar
foo_bar_baz    -> Foo bar baz
foo-bar        -> Foo-bar
fooBarBaz      -> FooBarBaz

Source: (StackOverflow)

Forcing "humanized" names of fields to be lowercase in Rails 3

As far as I know, the accepted way to set the "humanized" names of fields in Rails 3 is to use locales:

# config/locales/en.yml
en:
  activerecord:
    attributes:
      member:
        username: 'username' # rather than 'Username'

However, I simply want Rails 3 to use lowercase versions of its default humanized names. Is there an easy, built-in way to do this?

An example to help clarify: When inside of a form_for, <%= f.label :username %> displays "Username" by default. I want it to display "username".


Source: (StackOverflow)

django ifequal naturalday

I'm not sure why, but this condition will never evaluate True for me. I'm feeding it datetime.today() in the urls file. Am I missing something?

Template:

{% load humanize %}

{{ entry.date|naturalday }}  {# Evals to "today" #}

{% ifequal entry.date|naturalday "today" %}
    True
    {{ entry.date|date:"fA"|lower }} {{ entry.date|naturalday|title }}
{% else %}
    False
    {{ entry.date|naturalday|title }}
{% endifequal %}

Source: (StackOverflow)

How to properly calculate remaining time in JS using getTime() from the two dates?

I'm trying to calculate remaining time (ex: 10 years, 2 months and 10 days from today(2014/03/02) in JS using this function:

var d2 = new Date(2024, 3, 12);
var d1 = new Date();
var d0 = new Date(1970, 0, 1);

var diff = new Date(d2.getTime() - (d1.getTime() + d0.getTime() ) );
var years = diff.getFullYear();
var months = diff.getMonth();
var days = diff.getDay();

alert("remaining time = " + years + " years, " + months + " months, " + days + " days.");

But instead of get the 10 years difference, I got 1980 years difference (though the days difference I understand that are produced buy the variation of days in months and years):

enter image description here

Is it possible to perform this "remaining time" operation using this strategy? If so, how to get the expected result?

Here the function in a JS shell: jsfiddle.net/3ra6c/


Source: (StackOverflow)

javascript custom humanize filter

I am stuck with creating custom humanize function for my project. My API is returing labels that I want to turn into more readable such as:

probabilityOfDefault

and I want to change it into

Probability Of Default

or

historicalDate

and change it into

Historical Date

So far I have written a function but it only changes the letters to upper case, it doesnt add space before every each. Here it is:

var humanize = function(property) {
  return property.replace(/_/g, ' ')
  .replace(/(\w+)/g, function(match) {
    return match.charAt(0).toUpperCase() + match.slice(1);
  });
};

I am not an expert in regular expersions, also I am not unaware of any libaries that could do this for me. Any help ?


Source: (StackOverflow)

Rails datetime interval humanize

I want to write beautiful datetime interval.

If just write something like

"from #{date_start.strftime('%d.%m.%Y %H:%M')} 
 till #{date_end.strftime('%d.%m.%Y %H:%M')}"`

But in some situations this will looking bad. In example:

from 14.08.2012 00:00 till 16.08.2012 00:00

What can I do to humanize it?


Source: (StackOverflow)

'd' instead of 'days' in Django naturaltime

I'm workin in a mobile application with DRF as backend and using naturaltime built-in function, but since this is a mobile app, screen space is a big problem (atleast for me). So for an arbitrary date I get:

"1 week, 2 days ago"

Which is nice, but I wonder if it is possible get

"1w, 2d ago"

I know I could replace words in string, but I hope there's a better way. Any suggestion? Thanks!


Source: (StackOverflow)