VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > Python基础教程 >
  • 开源项目Humanizer介绍(2)

" TimeSpan.FromDays(16).Humanize() => "2 weeks"

TimeSpan.Humanize有一个可选的precision参数,它允许您指定返回值的精度。 精度的默认值为1,这意味着仅返回最大的时间单位,如您在TimeSpan.FromDays(16).Humanize()中看到的那样。 以下是一些指定精度的示例:

TimeSpan.FromDays(1).Humanize(precision:2) => "1 day" // no difference when there is only one unit in the provided TimeSpan
TimeSpan.FromDays(16).Humanize(2) => "2 weeks, 2 days"

// the same TimeSpan value with different precision returns different results
TimeSpan.FromMilliseconds(1299630020).Humanize() => "2 weeks"
TimeSpan.FromMilliseconds(1299630020).Humanize(3) => "2 weeks, 1 day, 1 hour"
TimeSpan.FromMilliseconds(1299630020).Humanize(4) => "2 weeks, 1 day, 1 hour, 30 seconds"
TimeSpan.FromMilliseconds(1299630020).Humanize(5) => "2 weeks, 1 day, 1 hour, 30 seconds, 20 milliseconds"

默认情况下,使用精度参数时,空时间单位不计入返回值的精度。 如果您不需要这种行为,则可以将重载的TimeSpan.Humanize方法与countEmptyUnits参数一起使用。 前导的空时间单位永远不会计数。 这是显示空单位计数的区别的示例:

TimeSpan.FromMilliseconds(3603001).Humanize(3) => "1 hour, 3 seconds, 1 millisecond"
TimeSpan.FromMilliseconds(3603001).Humanize(3, countEmptyUnits:true) => "1 hour, 3 seconds"

此方法有许多本地化版本:

// in de-DE culture
TimeSpan.FromDays(1).Humanize() => "Ein Tag"
TimeSpan.FromDays(2).Humanize() => "2 Tage"

// in sk-SK culture
TimeSpan.FromMilliseconds(1).Humanize() => "1 milisekunda"
TimeSpan.FromMilliseconds(2).Humanize() => "2 milisekundy"
TimeSpan.FromMilliseconds(5).Humanize() => "5 milisekúnd"

可以明确指定要使用的文化。 如果不是,则使用当前线程的当前UI文化。 例:

TimeSpan.FromDays(1).Humanize(culture: "ru-RU") => "один день"

另外,可以指定最短的时间单位,以避免滚动到较小的单位。 例如:

TimeSpan.FromMilliseconds(122500).Humanize(minUnit: TimeUnit.Second) => "2 minutes, 2 seconds"    // instead of 2 minutes, 2 seconds, 500 milliseconds
TimeSpan.FromHours(25).Humanize(minUnit: TimeUnit.Day) => "1 Day"   //instead of 1 Day, 1 Hour

另外,可以指定最大时间单位以避免累加到下一个最大单位。 例如:

TimeSpan.FromDays(7).Humanize(maxUnit: TimeUnit.Day) => "7 days"    // instead of 1 week
TimeSpan.FromMilliseconds(2000).Humanize(maxUnit: TimeUnit.Millisecond) => "2000 milliseconds"    // instead of 2 seconds

默认的maxUnit为TimeUnit.Week,因为它可以提供准确的结果。 您可以将此值增加到TimeUnit.Month或TimeUnit.Year,这将为您提供基于每年365.2425天和每月30.436875天的近似值。 因此,月份的间隔为30到31天,每四年为366天。

TimeSpan.FromDays(486).Humanize(maxUnit: TimeUnit.Year, precision: 7) => "1 year, 3 months, 29 days" // One day further is 1 year, 4 month
TimeSpan.FromDays(517).Humanize(maxUnit: TimeUnit.Year, precision: 7) => "1 year, 4 months, 30 days" // This month has 30 days and one day further is 1 year, 5 months

如果有多个时间单位,则使用“,”字符串将它们组合起来:

TimeSpan.FromMilliseconds(1299630020).Humanize(3) => "2 weeks, 1 day, 1 hour"

当TimeSpan为零时,默认行为将返回“ 0”加上最小时间单位。 但是,如果在调用Humanize时将true分配给toWords,则该方法将返回“ no time”。 例如:

TimeSpan.Zero.Humanize(1) => "0 milliseconds"
TimeSpan.Zero.Humanize(1, toWords: true) => "no time"
TimeSpan.Zero.Humanize(1, minUnit: Humanizer.Localisation.TimeUnit.Second) => "0 seconds"

使用collectionSeparator参数,可以指定自己的分隔符字符串:

TimeSpan.FromMilliseconds(1299630020).Humanize(3, collectionSeparator: " - ") => "2 weeks - 1 day - 1 hour"

也可以使用当前区域性的集合格式化程序来组合时间单位。 为此,将null指定为collectionSeparator参数:

// in en-US culture
TimeSpan.FromMilliseconds(1299630020).Humanize(3, collectionSeparator: null) => "2 weeks, 1 day, and 1 hour"

// in de-DE culture
TimeSpan.FromMilliseconds(1299630020).Humanize(3, collectionSeparator: null) => "2 Wochen, Ein Tag und Eine Stunde"

如果单词优先于数字,则可以设置toWords:true参数,以将人性化的TimeSpan中的数字转换为单词:

TimeSpan.FromMilliseconds(1299630020).Humanize(3,toWords:true)=>“两个星期,一天,一个小时”

 

10、人性化集合

您可以在任何IEnumerable上调用Humanize,以获取格式正确的字符串,该字符串表示集合中的对象。 默认情况下,将在每个项目上调用ToString()以获取其表示形式,但是可以将格式化函数传递给Humanize。 此外,提供了一个默认的分隔符(英语中为“ and”),但是可以将其他分隔符传递给Humanize。

例如:

class SomeClass
{
    public string SomeString;
    public int SomeInt;
    public override string ToString()
    {
        return "Specific String";
    }
}

string FormatSomeClass(SomeClass sc)
{
    return string.Format("SomeObject #{0} - {1}", sc.SomeInt, sc.SomeString);
}

var collection = new List<SomeClass>
{
    new SomeClass { SomeInt = 1, SomeString = "One" },
    new SomeClass { SomeInt = 2, SomeString = "Two" },
    new SomeClass { SomeInt = 3, SomeString = "Three" }
};

collection.Humanize()                                    // "Specific String, Specific String, and Specific String"
collection.Humanize("or")                                // "Specific String, Specific String, or Specific String"
collection.Humanize(FormatSomeClass)                     // "SomeObject #1 - One, SomeObject #2 - Two, and SomeObject #3 - Three"
collection.Humanize(sc => sc.SomeInt.Ordinalize(), "or") // "1st, 2nd, or 3rd"

修剪项目,并跳过空白(NullOrWhitespace)项目。 这导致干净的逗号标点。 (如果有自定义格式器功能,则此功能仅适用于格式器的输出。)

您可以通过实现ICollectionFormatter并向Configurator.CollectionFormatters注册它来提供自己的集合格式化程序。

 

11、Inflector 方法

还有一些 inflector 方法:

 

复数

在考虑不规则和不可数词的情况下,将提供的输入复数化:

"Man".Pluralize() => "Men"
"string".Pluralize() => "strings"

通常,您将对单个单词调用Pluralize,但如果不确定单词的奇异性,则可以使用可选的inputIsKnownToBeSingular参数调用该方法:

"Men".Pluralize(inputIsKnownToBeSingular: false) => "Men"
"Man".Pluralize(inputIsKnownToBeSingular: false) => "Men"
"string".Pluralize(inputIsKnownToBeSingular: false) => "strings"

具有复数参数的Pluralize重载已过时,在2.0版中已删除。

 

单数化

单数将提供的输入单数化,同时考虑不规则和不可数的单词:

"Men".Singularize() => "Man"
"strings".Singularize() => "string"

通常,您会在一个复数单词上调用单数化,但是如果不确定该单词的复数形式,则可以使用可选的inputIsKnownToBePlural参数调用该方法:

"Men".Singularize(inputIsKnownToBePlural: false) => "Man"
"Man".Singularize(inputIsKnownToBePlural: false) => "Man"
"strings".Singularize(inputIsKnownToBePlural: false) => "string"

具有复数参数的Singularize重载已过时,并且在2.0版中已删除。

 

12、添加单词

有时,您可能需要从单数/复数词汇表中添加一条规则(以下示例已在Inflector使用的默认词汇表中):

// Adds a word to the vocabulary which cannot easily be pluralized/singularized by RegEx.
// Will match both "salesperson" and "person".
Vocabularies.Default.AddIrregular("person", "people");

// To only match "person" and not "salesperson" you would pass false for the 'matchEnding' parameter.
Vocabularies.Default.AddIrregular("person", "people", matchEnding: false);

// Adds an uncountable word to the vocabulary.  Will be ignored when plurality is changed:
Vocabularies.Default.AddUncountable("fish");

// Adds a rule to the vocabulary that does not follow trivial rules for pluralization:
Vocabularies.Default.AddPlural("bus", "buses");

// Adds a rule to the vocabulary that does not follow trivial rules for singularization
// (will match both "vertices" -> "vertex" and "indices" -> "index"):
Vocabularies.Default.AddSingular("(vert|ind)ices$", "$1ex");

到数量

很多时候,您想调用单数化和复数化为单词加上数字。 例如 “ 2个请求”,“ 3个男人”。 ToQuantity为提供的单词加上数字前缀,并相应地对该单词进行复数或单数化:

"case".ToQuantity(0) => "0 cases"
"case".ToQuantity(1) => "1 case"
"case".ToQuantity(5) => "5 cases"
"man".ToQuantity(0) => "0 men"
"man".ToQuantity(1) => "1 man"
"man".ToQuantity(2) => "2 men"

ToQuantity可以判断输入单词是单数还是复数,并在必要时将单数或复数:

"men".ToQuantity(2) => "2 men"
"process".ToQuantity(2) => "2 processes"
"process".ToQuantity(1) => "1 process"
"processes".ToQuantity(2) => "2 processes"
"processes".ToQuantity(1) => "1 process"

您还可以将第二个参数ShowQuantityAs传递给ToQuantity,以指定希望如何输出提供的数量。 默认值为ShowQuantityAs.Numeric,这是我们在上面看到的。 其他两个值是ShowQuantityAs.Words和ShowQuantityAs.None。

"case".ToQuantity(5, ShowQuantityAs.Words) => "five cases"
"case".ToQuantity(5, ShowQuantityAs.None) => "cases"

还有一个重载,允许您格式化数字。 您可以传递要使用的格式和文化。

"dollar".ToQuantity(2, "C0", new CultureInfo("en-US")) => "$2 dollars"
"dollar".ToQuantity(2, "C2", new CultureInfo("en-US")) => "$2.00 dollars"
"cases".ToQuantity(12000, "N0") => "12,000 cases"

序数化

序数化将数字转换为序数字符串,用于表示有序序列(例如1st,2nd,3rd,4th)中的位置:


相关教程
关于我们--广告服务--免责声明--本站帮助-友情链接--版权声明--联系我们       黑ICP备07002182号